Using JTextArea to create lines - java

I am creating a program that display text using JTextArea and setLineWrap and then i want just three line display, but i don't know how set line limit.
I tried to used getColumn from TextArea, but that's not work.
that's just created new line forever
I wanted this:
But my result is this:
class FrameMaker extends JTextArea{
FrameMaker() {
JFrame frame = new JFrame();
JLabel label = new JLabel();
JPanel panel = new JPanel();
JTextArea area = new JTextArea(3, 10);
Dimension dms = new Dimension(500, 200);
int num = 0;
Font font = new Font("궁서", 1, 12);
frame.setPreferredSize(dms);
frame.pack();
frame.add(panel);
frame.setVisible(true);
panel.add(area);
area.setLineWrap(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int cnt = 1;
while(true) {
if(cnt == 10)
cnt = 1;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextTester.Text.append(cnt);
area.setText(TextTester.Text.toString());
cnt++;
}
}

Related

Why isn't the JPanel showing the ImageIcon?

I have a GridBagConstraints gbcImage and a JLabel that is initialized like this:
gbcImage.gridx = 1; // column 0
gbcImage.gridy = 2; // row 2
gbcImage.ipady = 100;
gbcImage.ipadx = 100;
JLabel label = new JLabel("", null, JLabel.CENTER);
label.setOpaque(true);
label.setBackground(Color.WHITE);
panel.add(label, gbcImage);
Where panel is added to a JFrame.
So I implemented a MouseListener to the label:
public void mouseClicked(MouseEvent e) {
JFileChooser jfc = new JFileChooser();
int iRet = jfc.showOpenDialog(panel);
if (iRet == jfc.APPROVE_OPTION)
{
File file = jfc.getSelectedFile();
try
{
BufferedImage bi = ImageIO.read(file);
image = new ImageIcon(bi);
JLabel label = new JLabel("", image, JLabel.CENTER);
panel.add(label, gbcImage);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
But it didn't work. The image doesn't show in the panel at runtime.
What am I missing?
There is no need to create a new JLabel. The problem is you added a new label to the panel but its default size is (0, 0) because you didn't reavalidate() and repaint() the panel.
There is no need to create a new label.
Instead you keep a reference to the original label (like you do for the panel) and then you just replace the icon:
image = new ImageIcon(bi);
label.setIcon( image );

Java layout when using JScrollPane

This is my code
private static KeyEvent e;
private static String text1 = null;
private static String text = null;
public fysikdel() {
super("Fysikformler");
setSize(700, 502);
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
setResizable(true);
setVisible(true);
}
public void init() {
JPanel main = new JPanel();
JPanel p = new JPanel();
CardLayout c1 = new CardLayout();
JScrollPane scrollpane = new JScrollPane(p);
JPanel Mekanik = new JPanel();
p.setSize(700, 502);
Mekanik.setLayout(new FlowLayout());
//637*237
ImageIcon likformigrorelsei = new ImageIcon();
JLabel likformigrorelsel = new JLabel();
ImageIcon lagesenergii = new ImageIcon();
JLabel lagesenergil = new JLabel();
ImageIcon a = new ImageIcon();
JLabel aa = new JLabel();
ImageIcon b = new ImageIcon();
JLabel bb = new JLabel();
try {
likformigrorelsei = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
likformigrorelsel.setIcon(likformigrorelsei);
try {
lagesenergii = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
lagesenergil.setIcon(lagesenergii);
try {
a = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
aa.setIcon(a);
try {
b = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
bb.setIcon(b);
Mekanik.add(likformigrorelsel);
Mekanik.add(lagesenergil);
Mekanik.add(aa);
Mekanik.add(bb);
JPanel Tryck = new JPanel();
main.setLayout(new GridLayout(1,1));
p.setLayout(c1);
this.add(main);
main.add(scrollpane);
p.add(Mekanik, "1");
p.add(Tryck, "2");
c1.show(p, "1");
When I add more pictures I want them to fill up from left to right untill one row is filled, then fill the next row. At the moment it just continue to fill the first row.
If I add
scrollpane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
it just removes the horizontal scroll bar but the pictures are still ending up on one row.
I dont know what is wrong. Thanks for any help!
So you put all your images in the Mekanik panel, which uses a FlowLayout. As the java tutorial states,
The FlowLayout class puts components in a row, sized at their preferred size
which is obviously not what you want. So you'll have to change the layout used by your panel.
To my mind, the GridLayout would be a better fit for your problem.

JPanels content doesn't reshow

I had spent two days in solving this problem but I have not found any solution.
I will put some easy code.
There is JFrame with some menu Bar, JPanels, but we focus on 2 options:
"Create Server"
"Close connection"
When I create server, on JPanel I draw checkerboard, and when I close connection, JPanel is clean, but the problem begins when I want to create server one more time, JPanel shows nothing.
I checked the input JPanel, and I put required JPanel before recreate server - nothing
I thought maybe it's gameControl, but I use it in other cases and it works also.
If anybody wants to try:
Here is entire project: http://bycniebytem.cba.pl/checkers.rar
Problem is inside the Checkers.java class,
But I'm waiting for your advices! Thanks!
hostServer.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
hostServer.setEnabled(false);
closeConnection.setEnabled(true);
server = new Server();
try
{
JPanel networkPanel = new JPanel();
player1 = new Player(1,boardSize, fieldSize);
player1.gameControl.setBounds(10, 10, boardSize*fieldSize + 10, boardSize*fieldSize + 10);
player1.gameControl.setSize(boardSize*fieldSize, boardSize*fieldSize);
player1.gameControl.addMouseListener(player1.gameControl);
player1.gameControl.addMouseMotionListener(player1.gameControl);
player1.gameControl.setFocusable(true);
player1.gameControl.setPreferredSize(new Dimension(boardSize*fieldSize,boardSize*fieldSize));
player1.connectWithServer(server.serverSocket.getInetAddress().getHostAddress());
JPanel votePanel = new JPanel();
JRadioButton whiteRadioButton = new JRadioButton("White");
JRadioButton blackRadioButton = new JRadioButton("Black");
blackRadioButton.setBounds(1, 1, 100, 30);
whiteRadioButton.setBounds(1, blackRadioButton.getHeight() + 5, 100, 30);
JButton bReady = new JButton("Ready");
bReady.setBounds(1, whiteRadioButton.getHeight() + 5, 100, 30);
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(whiteRadioButton);
radioGroup.add(blackRadioButton);
votePanel.add(blackRadioButton);
votePanel.add(whiteRadioButton);
votePanel.add(bReady);
votePanel.setBounds(player1.gameControl.getWidth() + 10, 10, blackRadioButton.getWidth(), blackRadioButton.getHeight() * 3);
votePanel.setPreferredSize(new Dimension(blackRadioButton.getWidth(), player1.gameControl.getHeight()));
setSize(boardSize * fieldSize + votePanel.getWidth() + 50, getJMenuBar().getHeight() + boardSize * fieldSize + 20);
networkPanel.add(player1.gameControl);
networkPanel.add(votePanel);
add(networkPanel);
setContentPane(networkPanel);
}
catch(IOException ex)
{
}
}
});
And close connection
closeConnection.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try
{
if(server != null)
{
if(server.getPlayerBlackSocket() != null)
{
server.sendMessage(server.getPlayerBlackSocket(), "closeConnection");
server.getPlayerBlackSocket().close();
}
if(server.getPlayerWhiteSocket() != null)
{
server.sendMessage(server.getPlayerWhiteSocket(), "closeConnection");
server.getPlayerWhiteSocket().close();
}
if(server.getServerSocket() != null)
{
server.getServerSocket().close();
}
}
}
catch (IOException ex)
{
}
player1.isOpenedConnection = false;
JPanel pane = (JPanel) getContentPane();
pane.removeAll();
remove(pane);
JPanel Pan = new JPanel();
JLabel label = new JLabel("label"); // just to test if jpanel can show and it shows
label.setBounds(10,10,100,30);
Pan.add(label);
setContentPane(Pan);
revalidate();
repaint();
closeConnection.setEnabled(false);
hostServer.setEnabled(true);
}
});

JTextArea is not being displayed

I have created a JFrame in which I have created JTextArea.I have passed this JTextArea as a constructor to the other class. The JFrame is as follows:
JFrame frame = new JFrame("Find");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JTextArea textArea = new JTextArea(20,15);
frame.add(textArea,BorderLayout.NORTH);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
Texts text = new Texts(textArea);
frame.add(text.pane(),BorderLayout.CENTER);
JScrollPane pane = new JScrollPane(textArea);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
JOptionPane.showMessageDialog(null, "Thank you for using finder");
System.exit(0); //Close program
}
});
}
I have passed the JTextArea to other class however it is not being displayed in the JFrame only the buttons show up.:
public class Texts {
public JTextArea tx;
JTextField findField = new JTextField( 10);
int pos = 0;
public Texts(JTextArea textArea) {
// TODO Auto-generated constructor stub
tx=textArea;
tx.setVisible(true);
}
public Component pane() {
// TODO Auto-generated method stub
JButton findButton = new JButton("Find");
JButton clearButton = new JButton("Clear");
JPanel header = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
header.add(findField, gbc);
gbc.gridx++;
header.add(findButton, gbc);
tx.add(header, BorderLayout.SOUTH);
header.add(clearButton);
findButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Get the text to find...convert it to lower case for easier comparison
String find = findField.getText();
// Focus the text area, otherwise the highlighting won't show up
tx.requestFocusInWindow();
// Make sure we have a valid search term
if (find != null && find.length() > 0) {
Document document = tx.getDocument();
..........
First you do this...
frame.add(textArea,BorderLayout.NORTH);
Then you do this...
JScrollPane pane = new JScrollPane(textArea);
This effectively removes the textArea from the frame, this is because a component can only have a single parent.
Instead, try something like...
JScrollPane pane = new JScrollPane(textArea);
frame.add(pane,BorderLayout.NORTH);

Making scrolls in GridBagLayout

Good time of the day, I'm trying to add scrolls to the GridBagLayout but definitely missing the correct way to do it.
The code:
public GUI(Map map)
{
//declare image icons and try to read them
ImageIcon missing = new ImageIcon();
ImageIcon wall = new ImageIcon();
ImageIcon floor = new ImageIcon();
//set texture for missing cases
try
{
Image tempImage = ImageIO.read(this.getClass().getResource("/resources/images/missing.png"));
missing = new ImageIcon(tempImage.getScaledInstance(16, 16, Image.SCALE_DEFAULT));
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
Image tempImage = ImageIO.read(this.getClass().getResource("/resources/images/wall.png"));
wall = new ImageIcon(tempImage.getScaledInstance(16, 16, Image.SCALE_DEFAULT));
tempImage = ImageIO.read(this.getClass().getResource("/resources/images/floor.png"));
floor = new ImageIcon(tempImage.getScaledInstance(16, 16, Image.SCALE_DEFAULT));
}
catch(Exception e)
{
e.printStackTrace();
}
Container pane = getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
for (int i=0;i<map.getMapSize();i++)
{
for (int j=0;j<map.getMapSize();j++)
{
c.gridy=i;
c.gridx=j;
if (i==0 && j==0)
{
c.weightx=1;
c.weighty=1;
}
else
{
c.weightx=0;
c.weighty=0;
}
c.gridwidth=1;
c.gridheight=1;
c.fill = GridBagConstraints.BOTH;
JLabel tile = new JLabel(missing);
if (map.getElementAt(i, j)==0)
{
tile.setIcon(wall);
}
else
{
tile.setIcon(floor);
}
pane.add(tile, c);
}
}
//c.gridx=map.getMapSize();
JScrollPane thePane = new JScrollPane();
pane.add(thePane, c);
setTitle("Game GUI");
setSize(640, 480);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
As far as I understand adding JScrollPane just after filling the pane is incorrect and of course it fails. The question is: how to add them correctly in this case? The aim overall I'm trying to achieve is to be able to scroll (move) the map in the game if it exceeds the game window size.
Thanks in advance:)
I guess the issue is that you try to add the GridBagConstraints to the JScollPane instead of adding your JPanel....
JScrollPane thePane = new JScrollPane();
pane.add(thePane, c);
try this:
JPanel myMapPanel = new Jpanel();
myMapPanel.setLayout(new GridBagLayout());
// add all the stuff to myMapPanel here (do the looping stuff here)
JScrollPane thePane = new JScrollPane(myMapPanel);
pane.add(thePane, c);
take care of this:
new JScrollPane(myMapPanel);

Categories

Resources