0
votes

I want to make my components on my JFrame resize with the resizing of the window.

How can this be done.

Currently when I make the JFrame smaller the components remain in the same place and then disappear as the frame gets smaller.

Ideally I would like to set a minimum JFrame size which would not allow the form to go any smaller but when I make it larger that components should move with the JFrame.

Add: What if I want to use an absolute layout manager. This is the layout manager that my boss prefers.

Code:

import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.JCheckBox; import java.awt.Font;

public class TestFrame extends JFrame { private static final long serialVersionUID = 1L; private JPanel contentPane; private JButton btnNewButton; private JButton btnNewButton_1; private JButton btnNewButton_2; private JCheckBox chckbxNewCheckBox; private JCheckBox chckbxNewCheckBox_1; private JCheckBox chckbxNewCheckBox_2;

public static void main(String[] args) {
    TestFrame frame = new TestFrame();
    frame.setVisible(true);
}

public TestFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 629, 458);

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    btnNewButton = new JButton("Save");
    btnNewButton.setFont(new Font("Sylfaen", Font.BOLD, 14));
    btnNewButton.setBounds(514, 386, 89, 23);
    contentPane.add(btnNewButton);

    btnNewButton_1 = new JButton("Open");
    btnNewButton_1.setBounds(514, 352, 89, 23);
    contentPane.add(btnNewButton_1);

    btnNewButton_2 = new JButton("Close");
    btnNewButton_2.setBounds(514, 318, 89, 23);
    contentPane.add(btnNewButton_2);
    btnNewButton_2.setBackground(Color.YELLOW);
    btnNewButton_2.setEnabled(false);

    chckbxNewCheckBox = new JCheckBox("Employeed");
    chckbxNewCheckBox.setBounds(506, 7, 97, 23);
    contentPane.add(chckbxNewCheckBox);

    chckbxNewCheckBox_1 = new JCheckBox("Not Employeed");
    chckbxNewCheckBox_1.setBounds(506, 33, 97, 23);
    contentPane.add(chckbxNewCheckBox_1);

    chckbxNewCheckBox_2 = new JCheckBox("Self Employeed");
    chckbxNewCheckBox_2.setBounds(506, 59, 97, 23);
    contentPane.add(chckbxNewCheckBox_2);

}

}

An Example of GridBagLayout:

This is an an example of the GridBagLayout. The components resize correctly but I do not see a grow property. It must do that automatically.

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class GridBagLayoutDemo {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    button = new JButton("Button 1");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 3");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 2;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Long-Named Button 4");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 40;      //make this component tall
    c.weightx = 0.0;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(button, c);

    button = new JButton("5");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0;       //reset to default
    c.weighty = 1.0;   //request any extra vertical space
    c.anchor = GridBagConstraints.PAGE_END; //bottom of space
    c.insets = new Insets(10,0,0,0);  //top padding
    c.gridx = 1;       //aligned with button 2
    c.gridwidth = 2;   //2 columns wide
    c.gridy = 2;       //third row
    pane.add(button, c);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("GridBagLayout");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addComponentsToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        createAndShowGUI();
    }
}
1
Setting the layout manger to null ("Absolute Positioning") disables the auto-resize behavior of the components. It is preferred (and easier) to use one of the stock layout managers that will handle this for you.Stephen Carlson

1 Answers

4
votes

Use a layout manager, like GridBagLayout, its have a property call "grow" for a column.