Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion

Go Back   Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion > Interests > Tilted Technology

Reply
 
LinkBack Thread Tools
Old 06-17-2004, 12:32 PM   #1 (permalink)
Rookie
 
Join Date: Jun 2004
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:     [                      ]
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!"
__________________
Wow.

Last edited by SunGun; 06-27-2004 at 10:29 PM.
SunGun is offline   Reply With Quote
Old 06-17-2004, 12:35 PM   #2 (permalink)
Rookie
 
Join Date: Jun 2004
Location: Ravenswood
And wow, none of the extra spacing I put in the whole document, for the programming section, and for the GUI sample of what I got and what I want did not come out right.. hmmm where'd my spaces go!!
__________________
Wow.
SunGun is offline   Reply With Quote
Old 06-26-2004, 02:52 AM   #3 (permalink)
Rookie
 
Join Date: Jun 2004
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
sargorn is offline   Reply With Quote
Old 06-26-2004, 10:47 PM   #4 (permalink)
Fluxing wildly...
 
MrFlux's Avatar
 
Join Date: Apr 2003
Location: Auckland, New Zealand
SunGun: that's what the [code] tag is for.
__________________
flux (n.)
Medicine. The discharge of large quantities of fluid material from the body, especially the discharge of watery feces from the intestines.
MrFlux is offline   Reply With Quote
Old 06-27-2004, 10:52 AM   #5 (permalink)
Rookie
 
Join Date: May 2003
Check out the NetBeans IDE, it comes with a layout called AbsoluteLayout. Lets you layout by pixel location.
bogosj13 is offline   Reply With Quote
Old 06-27-2004, 10:30 PM   #6 (permalink)
Rookie
 
Join Date: Jun 2004
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.
SunGun is offline   Reply With Quote
Old 07-10-2004, 01:47 AM   #7 (permalink)
Rookie
 
Join Date: Jul 2004
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).
DigitalD17 is offline   Reply With Quote
Old 07-26-2004, 09:27 PM   #8 (permalink)
Rookie
 
Join Date: Dec 2003
Another note about NetBeans: if you right-click on the layout in its Form Editor and choose Customize, you get a sort-of-WYSIWYG layout editor, which is a lot faster than setting up all the parameters by hand once you get used to it.
levity is offline   Reply With Quote
Reply

Bookmarks

Tags
form, gui, ick, java, laying

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -7. The time now is 06:05 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
All text (c) 2002-2008 Tilted Forum Project
"Insignia" vBulletin 3.5 - b6gm6n - x7x7x7.com