This is how it looks when I start my window:
And this is what I want:
This is the code I've used :
Main :
public void main()
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGui();
}
});
}
create and show gui
public static void createAndShowGui()
{
Frame chatPanel = new Frame();
JFrame frame = new JFrame("Chat");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(chatPanel);
//Ensure the frame is the minimum size it needs to display properly
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
Frame
public class Frame extends JPanel implements ActionListener
{
public static JTextArea textArea_send = new JTextArea(5, 14);
public static JTextArea textArea_receive = new JTextArea(15, 20);
private JButton send_button = new JButton("Send");
private JLabel receiver = new JLabel("Salon");
public Frame()
{
//Set the frame icon to an image loaded from a file.
//this.setIconImage(new ImageIcon(url).getImage());
JPanel AreaGrid = new JPanel();
AreaGrid.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gbc.gridy = 0; =
gbc.gridwidth = GridBagConstraints.REMAINDER; =
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(10, 15, 0, 0);
this.add(receiver, gbc);
/* Next component*/
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.;
gbc.weighty = 1.;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_START; // pas WEST.
gbc.insets = new Insets(30, 15, 0, 10);
this.add(new JScrollPane(textArea_receive,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), gbc);
/* next component */
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.BASELINE;
gbc.insets = new Insets(15, 15, 15, 10);
this.add(new JScrollPane(textArea_send,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), gbc);
/* Button */
gbc.gridx = 1;
this.add(send_button);
textArea_receive.setEditable(false);
textArea_send.setEditable(true);
send_button.addActionListener(this);
DefaultCaret caret = (DefaultCaret)textArea_receive.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
}
I would like my windows to stay organized like that, and if we expand or reduce it, only the two textArea would change size. For now, the textAreas do not resize.
Any recommendations are welcome !
Thanks in advance,
Victor
Your class extends JPanel
. The default layout manager for a JPanel is a FlowLayout
which is why the components are displayed the way they are.
JPanel AreaGrid = new JPanel();
AreaGrid.setLayout(new GridBagLayout());
The above code does nothing because you never add any components to the "areaGrid" panel. Get rid of those statements.
What you need to do is set the layout of the class itself since you add all the component directly to your custom class:
//JPanel AreaGrid = new JPanel();
//AreaGrid.setLayout(new GridBagLayout());
this.setLayout( new GridBagLayout() );