1

I'm trying to set up a couple of labels and textfields in a table-like style.

Currently I'm using the GridLayout, which kind of works as I want it to. However, the TextFields and Button is expanding to the full size of the cell.

How can I make the TextFields and Button "normal" sized, and what Layout Manager would accomplish this the easiest way?

Here's the current code and a screenshot:

    JPanel forms = new JPanel();
    forms.setLayout(new GridLayout(3, 2));

    JLabel lbl_navn = new JLabel("Virksomhedsnavn:");
    JTextField txt_navn = new JTextField();

    JLabel lbl_adresse = new JLabel("Adresse:");
    JTextField txt_adresse = new JTextField();

    forms.add(lbl_navn);
    forms.add(txt_navn);
    forms.add(lbl_adresse);
    forms.add(txt_adresse);
    forms.add(Box.createRigidArea(new Dimension(10, 10)));
    forms.add(new JButton("Opret virksomhed"));

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Patrick Reck
  • 11,246
  • 11
  • 53
  • 86
  • 3
    I'd recommend `GridBagLayout`. It gives you more power over how the components fill each cell. See [A Visual Guide to Layouts](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and [How to use GridBagLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) for more details – MadProgrammer Feb 14 '13 at 09:07

3 Answers3

1

I suggest you use gridBagLayout: http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

As an example: JPanel forms = new JPanel(); forms.setLayout(new GridBagLayout());

    JLabel lbl_navn = new JLabel("Virksomhedsnavn:");
    JTextField txt_navn = new JTextField();

    JLabel lbl_adresse = new JLabel("Adresse:");
    JTextField txt_adresse = new JTextField();
    //Setting grid bag constraints
    GridBagConstraints c = new GridBagConstraints();
    // Grid position coordinates
    c.gridx = 0; c.gridy = 0;
    //Align panel in top-left corner
    c.anchor=GridBagConstraints.LINE_START;
    forms.add(lbl_navn, c);
    c.gridx = 1; c.ipadx = 195;
    forms.add(txt_navn, c);
    c.gridy = 1; c.gridx = 0; c.ipadx = 0;
    forms.add(lbl_adresse, c);
    c.gridx = 1; c.ipadx = 195;
    forms.add(txt_adresse,c);
    c.gridy = 2; c.gridx     = 0; c.ipadx = 0;
    forms.add(Box.createRigidArea(new Dimension(10, 10)),c);


    c.anchor=GridBagConstraints.CENTER;
    c.gridy = 2; c.gridx = 1;
    forms.add(new JButton("Opret virksomhed"),c);

Hope this helps.

Deceiver
  • 324
  • 1
  • 2
  • 12
0

I think you can refer to this question where useful answers have been given: JTextField Fixed Height

Community
  • 1
  • 1
roqz
  • 1,020
  • 6
  • 17
0

I read good things about Mig layout a while ago, I never used, but seems very powerful. Check this Mig layout, may be a good solution for your problems.

UPDATE: Mig layout is not a "standard" layout from sun/oracle.

GridBagLayout as mentioned, can accomplished your objectives(from experience). I personally when I want fixed sizes components I use to work with "null layouts".

nms
  • 325
  • 1
  • 14