I'm trying to learn about GridBagLayout and I've gotten my TextArea components to behave just the way I want except for when the window is re-sized vertically.
When the window is re-sized to be shorter the bottom TextArea snaps to a larger size instead of keeping its current size and shrinking based on weighty when it runs out of room.
I feel like it's something simple like changing the fill or something but I can't figure it out..
Code:
public class Window {
Frame frame;
Panel panel;
TextArea top;
TextArea bottom;
GridBagConstraints topC, bottomC;
Window(){
createWindow();
}
public static void main(String[] args){
new Window();
}
private void createWindow(){
//create panel for text areas
panel = new Panel(new GridBagLayout());
//create top text area
top = new TextArea();
top.setPreferredSize(new Dimension(500,500));
//create bottom text area
bottom = new TextArea();
bottom.setPreferredSize(new Dimension(500,75));
//set constraints for top text area
topC = new GridBagConstraints();
topC.anchor = GridBagConstraints.NORTH;
topC.fill = GridBagConstraints.BOTH;
topC.gridheight = GridBagConstraints.RELATIVE;
topC.gridwidth = GridBagConstraints.REMAINDER;
topC.gridx = 0;
topC.gridy = 0;
topC.insets = new Insets(2,2,2,2);
topC.weightx = 1.0;
topC.weighty = 1.0;
//set constraints for bottom text area
bottomC = new GridBagConstraints();
bottomC.anchor = GridBagConstraints.SOUTH;
bottomC.fill = GridBagConstraints.BOTH;
bottomC.gridheight = GridBagConstraints.REMAINDER;
bottomC.gridwidth = GridBagConstraints.REMAINDER;
bottomC.gridx = 0;
bottomC.gridy = 1;
bottomC.insets = new Insets(2,2,2,2);
bottomC.weightx = 1.0;
bottomC.weighty = 0.5;
//add text areas to the panel
panel.add(top, topC);
panel.add(bottom, bottomC);
//create frame
frame = new JFrame();
frame.setTitle("Client Console");
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
frame.dispose();
}
});
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}