-
1 Attachment(s)
BorderLayout Sizes
I am trying to set the sizes of a BoarderLayout so that there is a JTextArea along the top of the screen and a TextField and JButton on the bottom (Side by side). Just like you would see in an instant messenger application.
I have tried settings the sizes by making a border layout and then changing the sizes of the components. What have I done wrong?
Does the layout adjust to the size changes of the components or not? Do I need to change the sizes of the layout, not the components?
Code:
public class GUI extends JFrame{
JTextField textInputField;
JTextArea textOutputField;
String textInput;
String textOutput;
public GUI(int width, int height){
BorderLayout layout = new BorderLayout();
textOutputField = new JTextArea();
textInputField = new JTextField();
JButton inputButton = new JButton();
setLayout(layout);
setVisible(true);
setSize(width, height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(textOutputField, BorderLayout.NORTH);
add(textInputField, BorderLayout.WEST);
add(inputButton, BorderLayout.EAST);
textOutputField.setSize(width, height/2);
textInputField.setSize(width/(4/5), height/2);
inputButton.setSize(width/(1/5), height/2);
textOutputField.setEditable(false);
textOutputField.setBackground(Color.GRAY);
textInputField.setBackground(Color.white);
}
It gives this:
Attachment 4462
-
Re: BorderLayout Sizes
Put the JTextArea in the CENTER; wrap the JTextField and JButton in another JPanel with any suitable layout (you have several options here) and add that to the SOUTH position.
Or learn to use GridBagLayout, with which your requirements can very easily be met.
db
-
Re: BorderLayout Sizes
Moved from New to Java
db
-
Re: BorderLayout Sizes
Is this right?
Because it gives me a messed up layout again.
Code:
public GUI(int width, int height){
BorderLayout layout = new BorderLayout();
textOutputField = new JTextArea();
textInputField = new JTextField();
JButton inputButton = new JButton();
JPanel inputPanel = new JPanel();
setLayout(layout);
setVisible(true);
setSize(width, height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
inputPanel.add(textInputField);
inputPanel.add(inputButton);
add(textOutputField, BorderLayout.CENTER);
add(inputPanel, BorderLayout.SOUTH);
textOutputField.setSize(width, height/2);
textInputField.setSize(width/(4/5), height/2);
inputButton.setSize(width/(1/5), height/2);
textOutputField.setEditable(false);
textOutputField.setBackground(Color.GRAY);
textInputField.setBackground(Color.white);
}
-
1 Attachment(s)
Re: BorderLayout Sizes
Attachment 4463
No matter what I do now, the JPanel at the bottom has that height and the JTextField has no width
Note: Ignore the exception too, that's just part of the bigger program. It doesn't have anything to do with the layout (The exception occurs after the GUI)
-
Re: BorderLayout Sizes
Ah, just found the issue.
it was simply becaase I had not set any character size for the JTextField and the Button had no text inside
-
Re: BorderLayout Sizes
You should never need setSize(...) and in fact the layout manager will ignore it.
I would use a BorderLayout for the inner JPanel, with the JTextField at CENTER and the JButton at EAST.
(Not really; I would use a GridBagLayout and wouldn't need an inner JPanel)
db