JTextField, JSeparator

I wrote a form using GridLayout; I would like to use JSeparator to separate different JTextFields:

formPanel =new JPanel(new GridLayout(0, 2));

M_Label =new JLabel("M = ");

M_Field =new JTextField(10);

D_Label =new JLabel("D = ");

D_Field =new JTextField(10);

formPanel.add(M_Label);

formPanel.add(M_Field);

JSeparator(SwingConstants.HORIZONTAL));

formPanel.add(D_Label);

formPanel.add(D_Field);

However, the layout changes: using JSeparator, the text fields and labels are shifted to the right and left...

What am I doing wrong?

[861 byte] By [SFLa] at [2007-11-27 6:23:24]
# 1

I think you're missing part of this line:

JSeparator(SwingConstants.HORIZONTAL));

Were you trying to add it to the panel there? If you want the separator to span across both columns of the layout, you could switch to GridBagLayout. It's more complicated, but you can specify how many rows/columns a component spans.

hunter9000a at 2007-7-12 17:41:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

I tried now GridBagLayout - without success:

JPanel formPanel = new JPanel(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

M_Label = new JLabel("M =");

c.gridx = 0;

c.gridy = 1;

formPanel.add(M_Label, c);

M_Field = new JTextField(10);

c.gridx = 1;

c.gridy = 1;

formPanel.add(M_Field, c);

formPanel.add(new JSeparator(SwingConstants.HORIZONTAL));

Insets in = new Insets(5,5,5,5);

D_Label = new JLabel("D =");

c.gridx = 0;

c.gridy = 2;

c.insets = in;

formPanel.add(D_Label, c);

D_Field = new JTextField(10);

c.gridx = 1;

c.gridy = 2;

formPanel.add(D_Field, c);

Using insets or not doesn't make a difference... I can't see the JSeparator.

SFLa at 2007-7-12 17:41:19 > top of Java-index,Desktop,Core GUI APIs...
# 3

hey,

you can't see it 'cause you don't add the jseparator to the grid....

c.gridx = 0;

c.gridy = 1;

c.gridwidth = 2; //span over 2 columns

c.fill = GridBagConstraints.HORIZONTAL; // fill the grid fully (horizontal)

formPanel.add(new JSeparator(SwingConstants.HORIZONTAL), c);

...something like that.... :)

izziieea at 2007-7-12 17:41:19 > top of Java-index,Desktop,Core GUI APIs...
# 4

Try this code.

public class SunExample29 extends JFrame {

public SunExample29() {

super("JSeparator Example");

JPanel formPanel = new JPanel();

formPanel.setLayout(new GridLayout(3,2,5,5));

JLabel M_Label = new JLabel("M = ");

formPanel.add(M_Label);

JTextField M_Field = new JTextField(10);

formPanel.add(M_Field);

formPanel.add(new JSeparator(SwingConstants.HORIZONTAL));

formPanel.add(new JSeparator(SwingConstants.HORIZONTAL));

JLabel D_Label = new JLabel("D = ");

formPanel.add(D_Label);

JTextField D_Field = new JTextField(10);

formPanel.add(D_Field);

getContentPane().add(formPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new SunExample29().setVisible(true);

}

}

AnanSmritia at 2007-7-12 17:41:19 > top of Java-index,Desktop,Core GUI APIs...
# 5

Thanks, I am now using the following code:

formPanel = new JPanel(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

Insets space = new Insets(5,5,5,5);

M_Label = new JLabel("M =");

c.gridx = 0;

c.gridy = 1;

formPanel.add(M_Label, c);

M_Field = new JTextField(10);

c.gridx = 1;

c.gridy = 1;

formPanel.add(M_Field, c);

D_Label = new JLabel("D =");

c.gridx = 0;

c.gridy = 2;

c.insets = space;

formPanel.add(D_Label, c);

D_Field = new JTextField(10);

c.gridx = 1;

c.gridy = 2;

formPanel.add(D_Field, c);

c.gridx = 0;

c.gridy = 3;

c.gridwidth = 2;

c.fill = GridBagConstraints.HORIZONTAL;

formPanel.add(new JSeparator(SwingConstants.HORIZONTAL), c);

(...)

I have a problem adding 2 JButtons... I tried the following:

okButton = new JButton("OK");

c.gridx = 0;

c.gridy = 14;

formPanel.add(okButton, c);

cancelButton = new JButton("Cancel");

c.gridx = 1;

c.gridy = 14;

formPanel.add(cancelButton, c);

The problem is that I can see the OK button, but not the Cancel button although I specify gridx and gridy correctly...?

SFLa at 2007-7-12 17:41:19 > top of Java-index,Desktop,Core GUI APIs...
# 6

see your modified version of the code. Better read the tutorial before placing your components using layouts.

JPanel formPanel = new JPanel(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

Insets space = new Insets(5,5,5,5);

JLabel M_Label = new JLabel("M =");

c.gridx = 0;

c.gridy = 1;

formPanel.add(M_Label, c);

JTextField M_Field = new JTextField(10);

c.gridx = 1;

c.gridy = 1;

formPanel.add(M_Field, c);

JLabel D_Label = new JLabel("D =");

c.gridx = 0;

c.gridy = 2;

c.insets = space;

formPanel.add(D_Label, c);

JTextField D_Field = new JTextField(10);

c.gridx = 1;

c.gridy = 2;

formPanel.add(D_Field, c);

c.gridx = 0;

c.gridy = 3;

c.gridwidth = 2;

c.fill = GridBagConstraints.HORIZONTAL;

formPanel.add(new JSeparator(SwingConstants.HORIZONTAL), c);

c.fill = GridBagConstraints.NONE; //added here

JButton okButton = new JButton("OK");

c.gridx = 0;

c.gridy = 14;

c.anchor = GridBagConstraints.LAST_LINE_START; //added here

formPanel.add(okButton, c);

JButton cancelButton = new JButton("Cancel");

c.gridx = 1;

c.gridy = 14;

c.anchor = GridBagConstraints.LAST_LINE_END; //added here

formPanel.add(cancelButton, c);

getContentPane().add(formPanel);

pack();

AnanSmritia at 2007-7-12 17:41:19 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thanks a lot! I first thought that GridBagLayout will automatically size the JButtons... :)
SFLa at 2007-7-12 17:41:19 > top of Java-index,Desktop,Core GUI APIs...