06-17-2004, 10:32 AM | #1 (permalink) |
Upright
Location: Ravenswood
|
[java] Laying out a form with java gui (ick)
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: [ ] 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!"
__________________
Wow. Last edited by SunGun; 06-27-2004 at 08:29 PM.. |
06-26-2004, 12:52 AM | #3 (permalink) |
Upright
Location: .au
|
Heh, it's hard to see what the problem is exactly. GridBagLayout is not a very friendly layout.
Have you tried more panels using different layouts? Maybe sticking GridLayout on a panel with 'Address:' and '[]', then another with 'City:' and '[]'. That's all I can think of off the top of my head... Hmm. David M |
06-27-2004, 08:30 PM | #6 (permalink) |
Upright
Location: Ravenswood
|
Thanks for telling me about the [code] tag.. I didnt know there was one lol...
In any event, I finally figured out GridBagLayout after about 4 days, pretty much got it down pat. I can lay out any GUI given to me on paper now, it's so easy to use once you know what your doing. If anyone needs help with it, give me a yell As for the AbsoluteLayout, i'll check that out, sounds good Thanks for the help guys SunGun
__________________
Wow. |
07-09-2004, 11:47 PM | #7 (permalink) |
Upright
|
What you probably want to do is to create a gridLayout with 4 panels in it, each oriented in NW, NE, SW, and SE (not sure of the keywords for those anchor positions), and put your labels and input boxes in each of those. It's kind of like an HTML table if you think about it, but not nearly as simple to set up. (at least in my opinion).
|
Tags |
form, gui, ick, java, laying |
|
|