Im currently using GridBagLayout to try and lay out a form such that the user can enter first name, last name, address, city, state, zip, etc
For the main display, i just leave it at default ( BorderLayout ), and what I do is I create each panel individually and then add each panel to some panel called center and add it to my main panel at BorderLayout.CENTER! GAH, enough of panels, heres an easier example:
Lets say I want a label with the word Address on it, and then right to its right a JTextField with 15 size:
Code:
/**********************************
This code is not copied out of the compiler,
but should be pretty clean of errors :\
I typed all this just now, for the post
Wow I had all this stuff tabbed out for readability,
I dont know why they went away :(
***********************************/
JFrame mainWindow = new JFrame("Main Window");
Container pane = mainWindow.getContentPane();
GridBagConstraints gbc = new GridBagConstraints();
GridBagConstraints labels = new GridBagConstraints();
GridBagConstraints textfield = new GridBagConstraints();
// I have an individual gbc for labels and text fields because their
// gridx's will always be the same. you'll see why
// I use the gbc constraints to add each individual panel to
// the center panel, and then i add the entire center panel
// onto the main panel
// Components are added to center first, and when im done
// working on center, i stick center on mainWindow in the center.
JPanel center = new JPanel( new GridBagLayout() );
JPanel address = new JPanel( new GridBagLayout() );
// This is the first component to get added to mainWindow,
// so the gridx and gridy will be at zero
gbc.gridx = 0; gbc.gridy =0;
address.add( new JLabel("Address: "), labels );
JTextField address_TXT = new JTextField(12);
address.add( address_TXT, textfield );
// Now we shall add the address panel to our temporary
// center panel, using our gbc constraints:
center.add( address, gbc );
// Now lets create a City label and text field box:
JPanel city = new JPanel( new GridBagConstraints() );
// This is the second component to get added to center,
// lets change the y's, but I want x at 0 cuz I would like
// all the components lined up on the y axis, left aligned
gbc.gridx = 0; gbc.gridy = 1;
city.add( new JLabel("City: "), labels );
JTextField city_TXT = new JTextField( 12 );
address.add( city_TXT, textfield );
// Now add this to our temp center panel:
center.add( city, gbc );
// Now our center panel contains the 2 separate panels -
// address, and city, right on top of each other. Now let's
// add that to the center panel of our mainWindow so its
// viewable by the users:
pane.add( center, BorderLayout.CENTER );
pane.setSize( 300,300 );
pane.show();
That above is just some scrap code of adding 2 labels and 2 jtextfields and thats the methodology im using (Ive only had java for one semester, so plz take it easy on me :) )
// The [ ] is the JTextField :)
My problem is, this is what I want the GUI to look like:
Address: [ ]
City: [ ]
Instead I get:
Address: [ ]
City: [ ]
It basicly looks like gbc is wanting to lay them out on the center pane in a Pyramid scheme, centered. Ive tried the gbc.fill = GridBagConstraints.BOTH, and that didnt work either...
I want them left aligned like the top one, can someone please help me out?
It would be greatly appreciated. I think Java gui is a pain, and every time I hit run, I say the age old statement,
"Hey it shouldnt look like that!"