JDialog, form, GridBagLayout
I am working on a form like shown in the Java tutorial: "How to Use SpringLayout", http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html -> "SpringForm".
I would like to use GridBagLayout. Here is my code:
publicclass TestDialogextends JDialog{
private JPanel infoPanel;
private JLabel infoLabel, nameLabel;
private JTextField nameField;
public TestDialog(){
Dimension dim_dialog =new Dimension(200, 100);
JPanel pane =new JPanel(new GridBagLayout());
GridBagConstraints c =new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
infoPanel =new JPanel();
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
pane.add(infoPanel, c);
infoLabel =new JLabel("Some information...");
infoPanel.add(infoLabel);
nameLabel =new JLabel("Name:");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
pane.add(nameLabel, c);
nameField =new JTextField(20);
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(nameField, c);
add(pane);
setPreferredSize(dim_dialog);
setDefaultCloseOperation( DISPOSE_ON_CLOSE );
pack();
}
}
As you can see, the layout is not correct; I would like to get a layout like in "SpringForm" - plus a JLabel at the top where I can write some informative text...
Can't figure it out at the moment how to use GridBagLayout for that :-/
Thanks!
[2142 byte] By [
SFLa] at [2007-11-27 5:40:46]

# 1
Well the page, whose link you provided, itself contains a link "How to use GridBag layout" try to go through it, and concentrate more on GridBagConstraints, reread if you don't understand at one go.
Now the code.
When you add the infoLabel the gridx and gridy both should be 0, and gridwidth should be 3, and weightx should be 1;
Note the call where you added the infolabel, you did not provide the GridBagConstraints there.
when you add other components, i think wightx should be 1, gridy should be 1, and gridx should be 0,1 & 2.
I think this will solve your problem.
# 2
I tried to correct my layout, however, I still has some errors in it:
public class TestDialog extends JDialog {
private JLabel infoLabel, nameLabel, addressLabel;
private JTextField nameField, addressField;
public TestDialog() {
Dimension dim_dialog = new Dimension(200, 100);
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
infoLabel = new JLabel("long text long text long text");
c.weightx = 0;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
pane.add(infoLabel, c);
nameLabel = new JLabel("Name:");
c.weightx = 1;
c.gridx = 1;
c.gridy = 1;
pane.add(nameLabel, c);
nameField = new JTextField(20);
c.weightx = 1;
c.gridx = 2;
c.gridy = 1;
pane.add(nameField, c);
addressLabel = new JLabel("Address:");
c.weightx = 1;
c.gridx = 1;
c.gridy = 2;
pane.add(addressLabel, c);
addressField = new JTextField(20);
c.weightx = 1;
c.gridx = 2;
c.gridy = 2;
pane.add(addressField, c);
add(pane);
setPreferredSize(dim_dialog);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
}
}
The textfields are stuck to the right; if I add more text to my infoLabel the layout gets even worse.
Where are my mistakes?
SFLa at 2007-7-12 15:17:32 >

# 3
with a horizontal fill, what else would you expect?c.fill = GridBagConstraints.NONE;c.anchor = GridBagConstraints.WEST; // can't hurt
# 4
oh, and if you set gridWidth=2 and you don't reset it to 1, then it's not going to reset automatically.
# 5
Not getting better :)
public TestDialog() {
Dimension dim_dialog = new Dimension(200, 100);
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.WEST;
infoLabel = new JLabel("long text long text long text");
c.weightx = 1;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
pane.add(infoLabel, c);
nameLabel = new JLabel("Name:");
c.weightx = 1;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 1;
pane.add(nameLabel, c);
nameField = new JTextField(20);
c.weightx = 1;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 1;
pane.add(nameField, c);
addressLabel = new JLabel("Address:");
c.weightx = 1;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 2;
pane.add(addressLabel, c);
addressField = new JTextField(20);
c.weightx = 1;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 2;
pane.add(addressField, c);
add(pane);
setPreferredSize(dim_dialog);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
}
}
Maybe I should use another layout manager?
SFLa at 2007-7-12 15:17:32 >

# 6
Well, there's also the issue of specifying gridx as 1 and 2, instead of 0 and 1 for your columns... Or not making infoLable's gridwidth = 3 to accommodate the implied 3 columns...
Dimension dim_dialog = new Dimension(200, 100);
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.WEST;
JLabel infoLabel = new JLabel("long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text");
c.weightx = 1;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
pane.add(infoLabel, c);
JLabel nameLabel = new JLabel("Name:");
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
pane.add(nameLabel, c);
JTextField nameField = new JTextField(20);
c.gridx = 1;
c.gridy = 1;
pane.add(nameField, c);
JLabel addressLabel = new JLabel("Address:");
c.gridx = 0;
c.gridy = 2;
pane.add(addressLabel, c);
JTextField addressField = new JTextField(20);
c.gridx = 1;
c.gridy = 2;
pane.add(addressField, c);
d.getContentPane().add(pane);
d.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
d.pack();
# 7
OK, the problem with the long text is solved... however, the text field is about one pixel wide :) I don't know why... even when I explicitly specify c.gridwidth = 1 there is no change... there is space enough...
Now I know why GridBagLayout "is one of the most complex layout managers".
SFLa at 2007-7-12 15:17:32 >

# 8
not when I ran that, it wasn't. Did you take the 20's (column widths) out of the fields?
# 9
No, it's exactly the same code you posted...?Should I use another Layout Manager?
SFLa at 2007-7-12 15:17:32 >

# 10
package com.sun.scjd.edu.gui;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestDialog extends JDialog {
private JPanel infoPanel;
private JLabel infoLabel, nameLabel,faxLabel,emailLabel,addressLabel;
private JTextField nameField,faxField,emailField,addressField;
public TestDialog() {
this.setTitle("Gridbag Layout Example");
Dimension dim_dialog = new Dimension(200, 100);
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//c.fill = GridBagConstraints.HORIZONTAL;
infoPanel = new JPanel();
//c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
pane.add(infoPanel, c);
infoLabel = new JLabel("Some information...");
c.gridx = 1;
c.gridy = 1;
infoPanel.add(infoLabel, c);
nameLabel = new JLabel("Name:");
//c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
pane.add(nameLabel, c);
nameField = new JTextField(10);
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(nameField, c);
Insets in = new Insets(5,5,5,5);
faxLabel = new JLabel("Fax:");
c.gridx = 0;
c.gridy = 2;
c.insets = in;
pane.add(faxLabel, c);
faxField = new JTextField(10);
c.gridx = 1;
c.gridy = 2;
pane.add(faxField, c);
emailLabel = new JLabel("Email:");
c.gridx = 0;
c.gridy = 3;
pane.add(emailLabel, c);
emailField = new JTextField(10);
c.gridx = 1;
c.gridy = 3;
pane.add(emailField, c);
addressLabel = new JLabel("Address:");
c.gridx = 0;
c.gridy = 4;
pane.add(addressLabel, c);
addressField = new JTextField(10);
c.gridx = 1;
c.gridy = 4;
pane.add(addressField,c);
add(pane);
setPreferredSize(dim_dialog);
setDefaultCloseOperation( DISPOSE_ON_CLOSE );
setSize(600,300);
}
public static void main(String[] args) {
new TestDialog().setVisible(true);
}
}
Did you expect this ?
# 11
Yeah, exactly :)
SFLa at 2007-7-12 15:17:32 >

# 12
Your problem is that the JTextField has also the
c.fill = GridBagConstraints.NONE
flag set so it will use its requested size and not that of one cell in the GridBag Layout. Set the option to
c.fill = GridBagConstraints.BOTH
and it will take the space of the complete cell.