What is the best way to lay out these components in Swing?

What would be the best approach / LayoutManager for JPanel, which was to display the components shown in the image? (Note: margins are resized horizontally if the window is resized, but not labels).

I'm currently trying (and struggling) to do this with a GridBagLayout. Is this the right approach or is there a better way? Should I try to split + subdue individual sections, maybe?

Any thoughts or advice would be greatly appreciated. :-)

http://img261.imageshack.us/img261/2091/layouthb6.png

PS If you read this and there is no image, please carry me while I fix it!

+5
source share
13 answers

JPanels. JPanel, JPanels.

GridBagLayout. , .

GridBagLayout, , GridBagConstraint .gridx, GridBagConstraint.gridx = 0 GridBagConstraint.gridy++ .

+11

. MigLayout ( -) JGoodies FormLayout, .

, ( GroupLayout, Matisse Netbeans, ). IMHO.

+4

DesignGridLayout:

DesignGridLayout layout = new DesignGridLayout(this);
layout.row().grid(label0).add(field0);
layout.emptyRow();
layout.row().grid(label1).add(field1);
layout.row().grid(label2).add(field2);
layout.row().grid(label3).add(field3);
layout.row().grid(label4).add(field4);
layout.emptyRow();
layout.row().grid(label5).add(field5).grid(label6).add(field6);
layout.row().grid(label7).add(field7).grid(label8).add(field8);
layout.emptyRow();
layout.row().grid(label9).add(field9);

: DesignGridLayout.

+2

, JGoodies .

, GridBagLayout , - , , .

+1

GroupLayout.

, , , .

+1

TableLayout , , TableLayout.FILL .

TableLayout

0

, "". :

  • Box.createVerticalBox(); - ,
  • : , JPanel BorderLayout ( , ) Box.createHorizontalBox() , /
  • "" ,
  • / , "" .

GridBagLayout . , , , GridBagLayout...

0

swing, Netbeans gui. . http://rghosting.co.cc

0

Java SE 6 GroupLayout

, .

:

alt text http://img165.imageshack.us/img165/8237/sampleusinggrouplayoutca2.png

:

    // Layout the components using the new GroupLayout added
    // in java 1.6.
    // Adding to the main frame
    private void layoutComponents(){
        JPanel panel = new JPanel();

        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        layout.setAutoCreateGaps(true);         
        layout.setAutoCreateContainerGaps(true);

        SequentialGroup hGroup = layout.createSequentialGroup();

        JLabel nameLbl  = new JLabel("Name");
        JLabel countLbl = new JLabel("Amount");
        JLabel dateLbl  = new JLabel("Date(dd/MM/yy)");
        hGroup.addGroup(layout.createParallelGroup().
                addComponent(nameLbl).
                addComponent(countLbl).
                addComponent(dateLbl).
                addComponent(go));

        hGroup.addGroup(layout.createParallelGroup().
                addComponent(name).
                addComponent(count).
                addComponent(date));

        layout.setHorizontalGroup(hGroup);

        SequentialGroup vGroup = layout.createSequentialGroup();

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(nameLbl).
                addComponent(name));

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(countLbl).
                addComponent(count));

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(dateLbl).
                addComponent(date));

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(go));
        layout.setVerticalGroup(vGroup);

        frame.add( panel , BorderLayout.NORTH );
        frame.add( new JScrollPane( textArea ) );
    }
0

GridBagLayout, , . NetBeans , , . IDE, .

0

GridBagLayout, .

, JPanel. ( , , , , , / )

0 - 4 9 gridwidth 4, - (1, 1)

"" .

( , ) . GridBagLayout . ( ), .

Visual Editor + Eclipse . ( Netbeans . , . , Eclipse)

0

, GridBagLayout , .

- , GridBagConstraints , , addToLayout.

IDE, , (, Matisse NetBeans).

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridBagLayoutTest extends JPanel {

    public GridBagLayoutTest() {
        setLayout(new GridBagLayout());
        addToLayout(new JLabel(     "Label 0" ), 0, 0, 0d, 1);
        addToLayout(new JTextField( "Field 0" ), 0, 1, 1d, 4);
        addToLayout(new JLabel(     "*"       ), 0, 5, 0d, 1);
        addToLayout(new JPanel()               , 1, 0, 0d, 1);
        addToLayout(new JLabel(     "Label 1" ), 2, 0, 0d, 1);
        addToLayout(new JTextField( "Field 1" ), 2, 1, 1d, 4);
        addToLayout(new JLabel(     "*"       ), 2, 5, 0d, 1);
        addToLayout(new JLabel(     "Label 2" ), 3, 0, 0d, 1);
        addToLayout(new JTextField( "Field 2" ), 3, 1, 1d, 1);
        addToLayout(new JLabel(     "*"       ), 3, 2, 0d, 1);
        addToLayout(new JLabel(     "Label 3" ), 3, 3, 0d, 1);
        addToLayout(new JTextField( "Field 3" ), 3, 4, 1d, 1);
        addToLayout(new JLabel(     "*"       ), 3, 5, 0d, 1);
    }

    private void addToLayout(java.awt.Component comp, int y, int x, double weightx, int gridwidth) {
        add(comp, new GridBagConstraints(x, y, gridwidth, 1, weightx, 0d,
                                         GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                                         new java.awt.Insets(2, 4, 2, 4), 0, 0 ) );
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new GridBagLayoutTest());
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
}
0

All Articles