I am trying to put together a panel which needs to consist of 20 lines,
each containing :
JLabel - 2 columns
JTextField - 1 colomn
JTextField - 1 column
JLabel - 2 Columns
The JText fields would be filled out from arrays, so it would be layed
out with the help of a 'for' loop.
I put together a test program (provided below) to try to emulate this,
but the layout manager only seems to want to give each entity (JLabel,
JTextField) the same amount of width each regardless of what I specify
in the GridWidth fields. Can this be done with specifying sizes for the
individual JTextfields or labels? Please advise - Thanks - Lou
import java.awt.*;
import javax.swing.*;
public class Test_GridBag2 {
public static void addComponentsToPane(JPanel pane) {
JButton button;
JLabel label;
JTextField jtf;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
//pane.setPreferredSize(new Dimension(300,200));
for (int i = 0; i<3;i++)
{
label = new JLabel("test");
c.gridx = 0;
c.gridwidth = 2; //2 columns wide
c.gridy = i; //third row
c.weightx = 1.0;
//c.anchor = GridBagConstraints.EAST; //bottom of space
pane.add(label, c);
jtf = new JTextField();
c.gridx = 2; //aligned with button 2
c.gridwidth = 1; //2 columns wide
c.gridy = i;
c.weightx = 1.0; //request any extra vertical space
//c.anchor = GridBagConstraints.EAST;//third row
pane.add(jtf, c);
jtf = new JTextField();
c.gridx = 3; //aligned with button 2
c.gridwidth = 1; //2 columns wide
c.gridy = i;
c.weightx = 1.0; //request any extra vertical space
//c.anchor = GridBagConstraints.EAST;//third row
pane.add(jtf, c);
label = new JLabel("");
c.gridx = 4; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = i;
//c.insets = new Insets(0,0,0,10);
c.weightx = 1.0; //request any extra vertical space
//c.anchor = GridBagConstraints.EAST;//third row
pane.add(label, c);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
addComponentsToPane(panel);
frame.getContentPane().add( panel );
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Karsten Lentzsch - 21 Jan 2005 18:47 GMT
> I am trying to put together a panel which needs to consist of 20 lines,
> each containing :
[quoted text clipped - 9 lines]
> in the GridWidth fields. Can this be done with specifying sizes for the
> individual JTextfields or labels? Please advise - Thanks - Lou
The JGoodies Forms layout system contains
non-visual builder classes that assist you
in building such forms quickly and in a way
that the builder code is easy to read.
The Forms' FormLayout helps you specify
a precise layout with typically 2 lines of code.
Forms is open source and available at no charge.
See http://forms.dev.java.net/
A whitepaper and a presentation about advanced
layout issues are available here:
http://www.jgoodies.com/articles/
Your example would read like this with the Forms:
FormLayout layout = new FormLayout(<your column spec>);
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
for (<each row>) {
builder.append(label, 2);
builder.append(field1, field2);
builder.append(label, 2);
}
return builder.getPanel();
Best regards,
Karsten
Lou Lipnickey - 21 Jan 2005 18:57 GMT
Thanks, I am interested, however, in understanding GBL a bit more. - Lou
>> I am trying to put together a panel which needs to consist of 20
>> lines, each containing :
[quoted text clipped - 36 lines]
> Best regards,
> Karsten
A. Bolmarcich - 22 Jan 2005 00:48 GMT
> I am trying to put together a panel which needs to consist of 20 lines,
> each containing :
[quoted text clipped - 8 lines]
> JTextField) the same amount of width each regardless of what I specify
> in the GridWidth fields.
GridBagLayout does not impose any constraints between the widths of
different columns. Using a gridwidth of 2 simply means that the
component occupies two grid column. The widths of those grid columns
is based on the preferred (or minimum) widths of the components in
those columns.
In your example, the preferred widths of the JTextFields that span
two columns determine the widths of those two columns. GridBagLayout
is free to distribute the width of the JTextFields between the two
columns any way it wants to. What GridBagLayout does is to set 1) the
width of one column to the width needed by the JTextFields, 2) the
width of the other column to zero.
> Can this be done with specifying sizes for the
> individual JTextfields or labels? Please advise - Thanks - Lou
It can be done using GridBagLayout by setting the perferred and minimum
sizes for the JTextfields and JLabels. You can
1) Set the preferred and minimum widths of the JLabels to be twice that
of the JTextfields.
2) Use a weightx on the JLabels that is twice the weightx of the
JTextfields.
3) Use a gridwidth of 1 for all the component.
However, it would be better to use a layout manager that can impose a
constraint between the widths of different columns. TableLayout is one
example, see
http://java.sun.com/products/jfc/tsc/articles/tablelayout/
Lou Lipnickey - 22 Jan 2005 15:47 GMT
Thanks, very helpful - Lou
>>I am trying to put together a panel which needs to consist of 20 lines,
>>each containing :
[quoted text clipped - 41 lines]
>
> http://java.sun.com/products/jfc/tsc/articles/tablelayout/