GridBag gremlins!


I'm using the GridBagLayout on a JPanel to show columns of JTextFields and their corresponding JLabels (as the column headers).

Everything shows us nice and aligned in all of the columns, but the columns are not equally spaced!

The first three are in the position they should be, with the first component being a JLabel (holding a name for each row), but after that, the spacing gets larger (a little at first, then a lot, then less, but still a lot) between the other columns which are JTextFields.

I checked the gridx, the weight, and the width and all of them are as they should be (gridx goes 0, 2, 4, 8 ,10..width is 1, weight is 0.0).

Any idea of what it is I'm doing wrong



Here is a snippet of the code:

private void init()
{
this.setLayout(new GridBagLayout());
this.setAlignmentX(JPanel.CENTER_ALIGNMENT);

mainLabel.setFont(new Font("SanSerif", Font.BOLD, 12));
mainLabel.setForeground(labelsColor);

grandTotalTextField.setBackground(totalsColor);

createVariables();

//Now draw
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 2, 2 , 2);
gbc.gridx = 6;
gbc.gridy = 0;
gbc.ipadx = 0;
gbc.ipady = 0;

this.add(mainLabel, gbc);

gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy += 2;
gbc.gridwidth = 1;

this.add(photoLabel, gbc);

for(int x = 0; x < TOTAL_COLS; x++)
{
gbc.gridx += 2;

this.add(columnLabels[x], gbc);
}

for(int x = 0; x < TOTAL_ROWS; x++)
{
gbc.gridx = 0;
gbc.gridy++;

this.add(imageLabels[x], gbc);

for(int y = 0; y < TOTAL_COLS; y++)
{
gbc.gridx += 2;

this.add(valuesTextField[x][y], gbc);

}
}

gbc.gridy++;
gbc.gridx = 8;

this.add(totalLabel, gbc);

gbc.gridx += 2;

grandTotalTextField.setPreferredSize(preferredDim);
grandTotalTextField.setEnabled(false);

this.add(grandTotalTextField, gbc);

}
Posted On: Tuesday 23rd of October 2012 11:35:25 PM Total Views:  218
View Complete with Replies

RELATED TOPICS OF Swing / AWT / SWT PROGRAMMING LANGUAGE




Unable to resize panel when components are added at runtime to a panel with gridbag layout

I am a fresher to JAVA Swing I am trying an example problem to add components at runtime to a panel. I am able to add the components, but unable to resize the panel. Can someone please suggest me what I am missing here in the following code
VIEWS ON THIS POST

155

Posted on:

Friday 21st September 2012
View Replies!

How to add Jseprator in gridbag layout

Jseprator is not displaying like i wanted its not portioning it properly what changes should i make please help import javax.swing.*; import java.awt.*; import java.util.*; class TestMainFrame extends JFrame { JLabel l1=new JLabel(" : LISTNING"); JLabel l2=new JLabel(" : Please Select the file you want to read"); JToggleButton b1 = new JToggleButton("POWER"); JButton b2=new JButton("Open"); Container c; TestMainFrame(String title) { super(title); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,400); c=getContentPane(); GridBagLayout gbl=new GridBagLayout(); GridBagConstraints gbc=new GridBagConstraints(); gbc.fill=GridBagConstraints.BOTH; c.setLayout(gbl); gbc.gridx=0; gbc.gridy=0; gbc.gridheight=1; gbc.gridwidth=1; gbc.insets=new Insets(10,10,20,20); c.add(b1,gbc); gbc.gridx=0; gbc.gridy=2; gbc.gridheight=1; gbc.gridwidth=1; c.add(b2,gbc); gbc.gridx=1; gbc.gridy=0; gbc.gridheight=1; gbc.gridwidth=3; gbc.fill=GridBagConstraints.BOTH; c.add(l1,gbc); JSeparator sep = new JSeparator(JSeparator.HORIZONTAL); gbc.gridx = 0; gbc.gridy = 1; gbc.fill = GridBagConstraints.HORIZONTAL; sep.setPreferredSize(new Dimension(10,3)); //gbc.weighty = 0; c.add(sep,gbc); gbc.gridx=1; gbc.gridy=2; gbc.gridheight=1; gbc.gridwidth=3; gbc.fill=GridBagConstraints.BOTH; c.add(l2,gbc); pack(); setResizable(false); } public static void main(String args[]) { TestMainFrame b=new TestMainFrame("TestMainFrame"); } }
VIEWS ON THIS POST

88

Posted on:

Friday 28th September 2012
View Replies!

How to make the window (JPanel?) bigger in gridbaglayout

When I run this program, the window pops up and everything is smooshed (for lack of a better term) together. How do I change the size of it I'm using GridBagLayout.
VIEWS ON THIS POST

97

Posted on:

Saturday 29th September 2012
View Replies!

Want to know about border Layout and gridbag layout

I need help on java swing layouts............. Please help asap.
VIEWS ON THIS POST

119

Posted on:

Wednesday 17th October 2012
View Replies!

Help in gridbaglayout

Hi all The code below doesnt do what it has to do. I need to place the components on the top left of my panel. But the components be still in the center of panel after giving this also gbc.anchor=GridBagConstraints.NORTHWEST; It doent work until i set gbc.weightx=0.0; and gbc.weighty=0.0;. Here the components get at the top position but the gap in between them is increasing. Someone please help me out. The code is here. import java.io.*; import java.sql.*; import javax.swing.event.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class test extends JFrame { Container c; JLabel l1; JTextField t1; JPanel panel; public test() { c=getContentPane(); panel=new JPanel(); GridBagConstraints gbc = new GridBagConstraints(); GridBagLayout gl=new GridBagLayout(); panel.setLayout(gl); // gbc.weightx=1.0; // gbc.weighty=1.0; gbc.anchor=GridBagConstraints.NORTHWEST; l1=new JLabel("Name"); t1=new JTextField(25); gbc.gridy=0; panel.add(l1,gbc); panel.add(t1,gbc); getContentPane().add(panel); setSize(600,600); setVisible(true); } public static void main(String args[]) { new test(); } }
VIEWS ON THIS POST

85

Posted on:

Wednesday 17th October 2012
View Replies!

how to display textfield in a gridbaglayout

, Using a gridbag layout & constaints i want to place a small textfield and a button near to it.when the applet window is maximized the textfield will expand upto 40-50% of the window but the button will be of same size and near to the textfield without much change.I tried a lot but the text button when maximized is moving awat frm the textfield.Help Me.
VIEWS ON THIS POST

90

Posted on:

Wednesday 17th October 2012
View Replies!

resizing without using gridbaglayout

hey we've been using gridbaglayout in class now for quite sometime now, and including the professor thinks its overly complicated. I have now one question. When using gridbaglayout its possible to set the variables to let specific panels stretch horizontally and vertically. Is it possible to do this without using gridbagLayout. My professor sais it is, but that its more complicated someone please tell me he's wrong and if so, how would i go about doing so
VIEWS ON THIS POST

118

Posted on:

Thursday 18th October 2012
View Replies!

lookit this! An animation about using gridbag!

http://madbean.com/blog/2004/17/totallygridbag.html Suddenly null layout looks pretty damn good!
VIEWS ON THIS POST

70

Posted on:

Thursday 18th October 2012
View Replies!