Grid Bag question.
From: Lou Lipnickey (lou.lipnickey_at_pobox.com)
Date: 01/21/05
- Next message: zach.dennis_at_gmail.com: "Re: JScrollPane and JLayeredPane"
- Previous message: Pooja B via JavaKB.com: "is it possible to have different no of columns for each row in JTable?"
- Next in thread: Karsten Lentzsch: "Re: Grid Bag question."
- Reply: Karsten Lentzsch: "Re: Grid Bag question."
- Reply: A. Bolmarcich: "Re: Grid Bag question."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 21 Jan 2005 18:18:02 GMT
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();
}
});
}
}
- Next message: zach.dennis_at_gmail.com: "Re: JScrollPane and JLayeredPane"
- Previous message: Pooja B via JavaKB.com: "is it possible to have different no of columns for each row in JTable?"
- Next in thread: Karsten Lentzsch: "Re: Grid Bag question."
- Reply: Karsten Lentzsch: "Re: Grid Bag question."
- Reply: A. Bolmarcich: "Re: Grid Bag question."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|