tags:

views:

66

answers:

1

I haven't understood this since I had started working with Swing. Why BoxLayout with Y-Axis sorting places (from the example) buttons one under another with space all the way to the end of panel (that's what I thought how it works) but placing JPanels same way but making them stretch. Would someone be so kind to explain to me the phenomenon? (and yes the example code is generated, but that's so I'm not writing it even now :-))


public class MyFrame extends JFrame {

    private JButton jButton1;
    private JButton jButton2;
    private JLabel jLabel1;
    private JLabel jLabel10;
    private JLabel jLabel11;
    private JLabel jLabel12;
    private JLabel jLabel13;
    private JLabel jLabel14;
    private JLabel jLabel15;
    private JLabel jLabel16;
    private JLabel jLabel17;
    private JLabel jLabel18;
    private JLabel jLabel19;
    private JLabel jLabel2;
    private JLabel jLabel20;
    private JLabel jLabel21;
    private JLabel jLabel22;
    private JLabel jLabel23;
    private JLabel jLabel24;
    private JLabel jLabel25;
    private JLabel jLabel26;
    private JLabel jLabel27;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JLabel jLabel7;
    private JLabel jLabel8;
    private JLabel jLabel9;
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JPanel jPanel3;
    private JPanel jPanel4;
    private JScrollPane jScrollPane1;
    private JScrollPane jScrollPane2;

    public MyFrame() {
        buildGUI();
        setVisible(true);
    }

    private void buildGUI() {

        jScrollPane1 = new JScrollPane();
        jPanel4 = new JPanel();
        jPanel2 = new JPanel();
        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        jLabel3 = new JLabel();
        jLabel4 = new JLabel();
        jLabel5 = new JLabel();
        jLabel10 = new JLabel();
        jLabel9 = new JLabel();
        jLabel8 = new JLabel();
        jLabel7 = new JLabel();
        jLabel6 = new JLabel();
        jLabel27 = new JLabel();
        jPanel3 = new JPanel();
        jLabel11 = new JLabel();
        jLabel12 = new JLabel();
        jLabel13 = new JLabel();
        jLabel14 = new JLabel();
        jLabel15 = new JLabel();
        jLabel16 = new JLabel();
        jLabel17 = new JLabel();
        jLabel18 = new JLabel();
        jLabel19 = new JLabel();
        jLabel20 = new JLabel();
        jLabel21 = new JLabel();
        jLabel22 = new JLabel();
        jLabel26 = new JLabel();
        jLabel25 = new JLabel();
        jLabel24 = new JLabel();
        jLabel23 = new JLabel();
        jScrollPane2 = new JScrollPane();
        jPanel1 = new JPanel();
        jButton1 = new JButton();
        jButton2 = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jPanel4.setLayout(new BoxLayout(jPanel4, BoxLayout.Y_AXIS));

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("jLabel1");
        jPanel2.add(jLabel1);

        jLabel2.setText("jLabel2");
        jPanel2.add(jLabel2);

        jLabel3.setText("jLabel3");
        jPanel2.add(jLabel3);

        jLabel4.setText("jLabel4");
        jPanel2.add(jLabel4);

        jLabel5.setText("jLabel5");
        jPanel2.add(jLabel5);

        jLabel10.setText("jLabel10");
        jPanel2.add(jLabel10);

        jLabel9.setText("jLabel9");
        jPanel2.add(jLabel9);

        jLabel8.setText("jLabel8");
        jPanel2.add(jLabel8);

        jLabel7.setText("jLabel7");
        jPanel2.add(jLabel7);

        jLabel6.setText("jLabel6");
        jPanel2.add(jLabel6);

        jLabel27.setText("jLabel27");
        jPanel2.add(jLabel27);

        jPanel4.add(jPanel2);

        jLabel11.setText("jLabel11");
        jPanel3.add(jLabel11);

        jLabel12.setText("jLabel12");
        jPanel3.add(jLabel12);

        jLabel13.setText("jLabel13");
        jPanel3.add(jLabel13);

        jLabel14.setText("jLabel14");
        jPanel3.add(jLabel14);

        jLabel15.setText("jLabel15");
        jPanel3.add(jLabel15);

        jLabel16.setText("jLabel16");
        jPanel3.add(jLabel16);

        jLabel17.setText("jLabel17");
        jPanel3.add(jLabel17);

        jLabel18.setText("jLabel18");
        jPanel3.add(jLabel18);

        jLabel19.setText("jLabel19");
        jPanel3.add(jLabel19);

        jLabel20.setText("jLabel20");
        jPanel3.add(jLabel20);

        jLabel21.setText("jLabel21");
        jPanel3.add(jLabel21);

        jLabel22.setText("jLabel22");
        jPanel3.add(jLabel22);

        jLabel26.setText("jLabel26");
        jPanel3.add(jLabel26);

        jLabel25.setText("jLabel25");
        jPanel3.add(jLabel25);

        jLabel24.setText("jLabel24");
        jPanel3.add(jLabel24);

        jLabel23.setText("jLabel23");
        jPanel3.add(jLabel23);

        jPanel4.add(jPanel3);

        jScrollPane1.setViewportView(jPanel4);

        jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS));

        jButton1.setText("jButton1");
        jPanel1.add(jButton1);

        jButton2.setText("jButton2");
        jPanel1.add(jButton2);

        jScrollPane2.setViewportView(jPanel1);

        GroupLayout layout = GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(jScrollPane1, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE)
                .add(37, 37, 37)
                .add(jScrollPane2, GroupLayout.PREFERRED_SIZE, 244, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(21, 21, 21)
                .add(layout.createParallelGroup(GroupLayout.TRAILING)
                    .add(GroupLayout.LEADING, jScrollPane2, GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                    .add(GroupLayout.LEADING, jScrollPane1, GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
                .add(40, 40, 40))
        );

        pack();
    }

    public static void main(String args[]) {
        new MyFrame();
    }
}
+3  A: 

A JButton has a default maximum size equivalent to the preferred size (based on the text/icon in the button), which means it instructs the LayoutManager not to size it any bigger than its normal size.

A JPanel has a default maximum size of 32767 / 32767, which means it is fine taking up pretty much any space you give it.

You can take a look with this snippit:

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    for ( int i = 0; i < 5; i++ ) {
        JComponent comp = new JPanel();
//      JComponent comp = new JButton("Button " + i);
        System.out.printf("Pref: %s Max: %s%n", comp.getPreferredSize(), comp.getMaximumSize());
        comp.setBackground(new Color(100 + i * 30, 0, 0));
        panel.add(comp);
    }

The maximum size for both can be set explicitly using setMaximumSize().

Mark Peters
Thank you, now I understand it, I don't know if it's appropriate here because this boxlayout thing is part of my little more complex issue. Can include it here?
Martin S.
@Martin: If you have a more complex problem than that for the solution I gave, feel free to update your question. But be sure to get your code down to the smallest piece of code that demonstrates your more complex problem, and be specific about the problem too. The code that you posted was far too long for me to want to look at it.
Mark Peters
I came across another problem with this, I can set maximum size of the JPanel but the thing is it does always scale the panel to its maximum size when it's placed inside JPanel with BoxLayout and I don't know what the actual size will be because it depends on what scaled JPanel contains
Martin S.