Question

OK I have a panel, and I want to place two panels on to it, left and right, such that the right panel should be double the width of the left side panel.

I want to add a menu to the left side panel, and the details of the selected item will appear in the right side panel.

Secondly, the size of the panels and their components should increase proportionately when the window is expanded (If any methods can be used for the purpose, please suggest!)

I did try to use the GridBagLayout but I think I have not yet been able to grasp it well. So please suggest the simplest layout manager which can serve my purpose.


EDIT:- PROBLEMS WITH WHAT I HAD TRIED WITH THE GRIDBAG

//Set Layout
        setLayout(new GridBagLayout());
        GridBagConstraints c= new GridBagConstraints();

        //Set Layout constraints of components and add them to the MainPanel
        c.gridx=0;
        c.gridy=0;
        c.weightx=0.5;
        c.insets= new Insets(5,5,5,5);
        c.fill=GridBagConstraints.BOTH;
        add(iListPanel);
        iListPanel.setBorder(BorderFactory.createTitledBorder("Check") );

        GridBagConstraints c1= new GridBagConstraints();
        c.gridx=1;
        c.gridy=0;
        c.weightx=1;
        c.insets= new Insets(5,5,5,5);
        c.fill=GridBagConstraints.BOTH;
        add(iDetailsPanel);
        iDetailsPanel.setBorder(BorderFactory.createTitledBorder("Check"));

enter image description here

Was it helpful?

Solution

Of the layout managers that come with Java, I think GridBagLayout is the simplest one that will do that. It's worth the time to learn it, because it's about the only layout manager that's halfway-competent. This should do it:

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c;

JPanel left = new JPanel();
left.setBorder(BorderFactory.createTitledBorder("Left"));
c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
c.fill = c.BOTH;
panel.add(left, c);

JPanel right = new JPanel();
right.setBorder(BorderFactory.createTitledBorder("Right"));
c = new GridBagConstraints();
c.weightx = 2;
c.weighty = 1;
c.fill = c.BOTH;
panel.add(right, c);

However, from the description of your problem, it sounds like JSplitPane will serve you better for this. It's a ready-made component to do more-or-less what you're asking, and has a user-resizeable separator as well. An example:

JSplitPane pane = new JSplitPane();
pane.setResizeWeight(1/3f); // right will be twice size of left

JPanel left = new JPanel();
left.setBorder(BorderFactory.createTitledBorder("Left"));
pane.setLeftComponent(left);

JPanel right = new JPanel();
right.setBorder(BorderFactory.createTitledBorder("Right"));
pane.setRightComponent(right);

Edit: The problem with your original code is that it does not use the constraints.

add(iListPanel);

should be:

add(iListPanel, c);

and likewise for iDetailsPanel.

OTHER TIPS

You want to use a BorderLayout.

panel.add(panelLeft,"West");
panel.add(panelRight,"East");

North, South and Center is also available

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top