0

I have 4 JPanels in a dialog that are arranged vertically using GridBagLayout. All of them should have equal length ideally.

Every thing works fine except Panel 2 from the top whose contents are added and removed dynamically depending on the selection made by a combo-box that is also contained in the same Panel and are not of the fixed size. Due to the nature of the contents I use GridBagLayout for Panel 2 itself as well and initially set the Min., Max. and PreferredSize of the Panel to the largest posible size of the contents and wants this panel to stick to this size. But that doesn't work as Panel2 is also get effected by Panel1 and Panel2 contents(Their contents are also added and removed but always has the same size)

Any suggestion? Wanted to add the photo of the panels but coudn't due the strange restrictions imposed here

OK after viewing the comments on the question, i am going to add some code snippet here

    JPanel dataPanel=new JPanel();
    dataPanel.setLayout(new GridBagLayout());
    final GridBagConstraints constraintAmpliaciones = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
           GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);

   //dataPanel.add(panelIdOrigen, constraintAmpliaciones);

   constraintAmpliaciones.gridy++;
   dataPanel.add(panel1,constraintAmpliaciones );

   constraintAmpliaciones.gridy++;
   dataPanel.add(panel2,constraintAmpliaciones);

   constraintAmpliaciones.gridy++;
   dataPanel.add(panel3, constraintAmpliaciones);

   constraintAmpliaciones.gridy++;
   dataPanel.add(panel4,constraintAmpliaciones);

Where as Panel2(the problematic one) is some thing like this:

    void initComponents() {
        panelGeneral = new JPanel();
        panelGeneral.setLayout(new GridBagLayout());
        panelGeneral.add(panelLarger, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        /**
         *  here for dimension,I have tried different sizes including the fixed size in pixels of panelLarger
         */
        Dimension dimension = new Dimension((int) panelGeneral.getPreferredSize().getWidth(), (int) panelGeneral.getPreferredSize().getHeight());
        panelGeneral.setMaximumSize(dimension);
        panelGeneral.setPreferredSize(dimension);
        panelGeneral.setMinimumSize(dimension);

        this.add(panelGeneral);

    }

    /**
     * is called upon combox selection
     */
    void updateComponents() {
        if (selection1) {
            panelGeneral.remove(panelLarger);
            panelGeneral.add(panelSmaller, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));


        } else {
            panelGeneral.remove(panelSmaller);
            panelGeneral.add(panelLarger, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

        }
    }
mediterranean
  • 123
  • 1
  • 9
  • Could do with a runnable code example :) – Gorbles Aug 11 '14 at 09:50
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). – Andrew Thompson Aug 11 '14 at 10:04
  • *"..and initially set the Min., Max. and PreferredSize of the Panel"* See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Aug 11 '14 at 10:05
  • You have been a member for five months! You can change your user name. – Edwin Buck Aug 11 '14 at 13:25
  • Jaja... @Andrew Thompson for me the term MCVE is much more abstract than the question I have asked. I think data input restrictions are more valid for digital machines. We human beings are little more flexible and with diverse capabilities. Of course if I had Ideal MCVE level then I may be able to solve the problem myself. The intention of the forum should be that people thing in different ways and one can do things in a better way and other may not be – mediterranean Aug 12 '14 at 11:17

1 Answers1

0

I'm not sure I understand correctly, but you could try this:

Put a JLabel in Panel2 under all the other components in Panel2 (I'm assuming Panel2 is too small vertically when it doesn't have all the components it could have in it) . Make sure the Jlabel text is set to "" so it will appear invisible to the user.

After this, you want to set the GridBagConstraints of the JLabel to fill = VERTICAL and weighty = 1.0.

This should push all the other components to the top of Panel2 as the JLabel expands vertically underneath them, and it should preserve the size of the Panel and prevent it from shrinking. I suppose if you know exactly how big the image would have been each time, you could put in a JLabel of this size instead of having it scale vertically.

Swing can be tricky since I can't see all your code, but I frequently make use of hidden JLabels in my GUIs to make panels scale how I want them too.

mbw
  • 366
  • 3
  • 12
  • your solution is valid unless I don't have a combo box in the first column of the Grid. In Panel two I have two things. the combo box at column 1 and a panelGeneral as shown in the code snippet. Now the contents of panelGeneral itself are dynamically changed depending upon the selection made using combo box. If I don't want to fix the size of Panel2, every thing is OK within Panel2 as it size shrinks and grows according to its components but this is not desirable because Panel2 is a part of 4 Panels rows which got disturbed by resizing any of the 4 panels including Panel2. – mediterranean Aug 12 '14 at 11:11