Strokes
Strokes

Reputation: 217

Dynamic GridLayout with fixed size

Guidance required..

Would like to achieve something like the image below whereby the child panel stays the same size but contains up to 4 components. I realize I can achieve this by changing the number of columns in my gridlayout below but in order to keep the child panel the same size I would have to change the border sizes which is something I don't mind doing but it seems a bit cumbersome and am wondering if there is a smart way to go about this. The code I have provided is based on sample code provided to me here

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class DynamicGridLayout {

    private JPanel ui = null;

    DynamicGridLayout() {
        initUI();
    }

    public final void initUI() {
        if (ui!=null) return;
        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new TitledBorder("Parent Panel"));

        JPanel controls = new JPanel(new GridLayout(2,0,10,10));
        ui.add(controls);
        controls.setBackground(Color.RED);
        controls.setBorder(new TitledBorder("Child Panel"));

        for (int ii=1; ii<5; ii++) {
            addLabel(controls, "String " + ii);
        }
    } 

    public JComponent getUI() {
        return ui;
    }

    private void addLabel(JPanel panel, String text) {
        JPanel controls1 = new JPanel(new GridLayout(3,0,3,3));
        controls1.setBackground(Color.green);
        controls1.setBorder(new EmptyBorder(75,75,75,75));
        panel.add(controls1);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Three Button/Text Field Combo");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);
                DynamicGridLayout dgl = new DynamicGridLayout();
                f.setContentPane(dgl.getUI());
                f.setSize(1050, 720);
                f.setMinimumSize(f.getSize());
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 6

Views: 3909

Answers (1)

mKorbel
mKorbel

Reputation: 109823

  • just for my enjoy, fun,

  • note have to notify (deepest child required override for min/max/preferredsize from) parent JPanel after LayoutManager is switched back from GridLayout to BorderLayout (unwanted output to see in figure 5th.)

.

enter image description here

.

enter image description here

.

enter image description here

.

enter image description here

.

enter image description here

.

from

.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class DynamicGridLayout {

    private JFrame f = new JFrame("Three Button/Text Field Combo");
    private JPanel ui = new JPanel(new GridBagLayout()) {
        @Override
        public Dimension getMinimumSize() {
            return new Dimension(400, 300);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 600);
        }

        @Override
        public Dimension getMaximumSize() {
            return new Dimension(800, 600);
        }
    };
    private JPanel controls = new JPanel() {
        @Override
        public Dimension getMinimumSize() {
            return new Dimension(400, 300);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 600);
        }

        @Override
        public Dimension getMaximumSize() {
            return new Dimension(1050, 720);
        }
    };
    private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack;

    DynamicGridLayout() {
        initUI();
    }

    public final void initUI() {
        ui.setBorder(new TitledBorder("Parent Panel"));
        ui.add(controls);
        controls.setBackground(Color.RED);
        controls.setBorder(new TitledBorder("Child Panel"));
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setLocationByPlatform(true);
        f.add(ui);
        f.add(getCheckBoxPanel(), "South");
        f.setMinimumSize(ui.getPreferredSize());
        f.setVisible(true);
    }

    public JComponent getUI() {
        return ui;
    }

    private void addLabel() {
        JPanel controls1 = new JPanel(new GridLayout(3, 0, 3, 3));
        controls1.setBackground(Color.green);
        controls1.setBorder(new EmptyBorder(75, 75, 75, 75));
        controls.add(controls1);
    }

    private JPanel getCheckBoxPanel() {
        checkValidate = new JCheckBox("validate");
        checkValidate.setSelected(false);
        checkReValidate = new JCheckBox("revalidate");
        checkReValidate.setSelected(true);
        checkRepaint = new JCheckBox("repaint");
        checkRepaint.setSelected(true);
        checkPack = new JCheckBox("pack");
        checkPack.setSelected(false);
        JButton addComp = new JButton("Add New One");
        addComp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (controls.getComponentCount() < 1) {
                    controls.setLayout(new BorderLayout());
                    addLabel();
                    makeChange();
                } else if (controls.getComponentCount() == 1) {
                    controls.setLayout(new GridLayout(0, 2, 10, 10));
                    addLabel();
                    makeChange();
                } else {
                    controls.setLayout(new GridLayout(2, 0, 10, 10));
                    addLabel();
                    makeChange();
                }
                System.out.println(" Components Count after Adds :" + controls.getComponentCount());
            }
        });
        JButton removeComp = new JButton("Remove One");
        removeComp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int count = controls.getComponentCount();
                if (count > 0) {
                    if (controls.getComponentCount() == 2) {
                        controls.setLayout(new BorderLayout());
                        controls.remove(0);
                    } else if (controls.getComponentCount() == 3) {
                        controls.setLayout(new GridLayout(0, 2, 10, 10));
                        controls.remove(0);
                    } else {
                        controls.remove(0);
                    }

                }
                makeChange();
                System.out.println(" Components Count after Removes :" + controls.getComponentCount());
            }
        });
        JPanel panel2 = new JPanel();
        panel2.add(checkValidate);
        panel2.add(checkReValidate);
        panel2.add(checkRepaint);
        panel2.add(checkPack);
        panel2.add(addComp);
        panel2.add(removeComp);
        return panel2;
    }

    private void makeChange() {
        if (checkValidate.isSelected()) {
            ui.validate();
        }
        if (checkReValidate.isSelected()) {
            ui.revalidate();
        }
        if (checkRepaint.isSelected()) {
            ui.repaint();
        }
        if (checkPack.isSelected()) {
            f.pack();
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                DynamicGridLayout dgl = new DynamicGridLayout();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 12

Related Questions