Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [java] Laying out a form with java gui (ick) (https://thetfp.com/tfp/tilted-technology/59506-java-laying-out-form-java-gui-ick.html)

SunGun 06-17-2004 10:32 AM

[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?

:D

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!"

SunGun 06-17-2004 10:35 AM

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!!

sargorn 06-26-2004 12:52 AM

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

MrFlux 06-26-2004 08:47 PM

SunGun: that's what the [code] tag is for.

bogosj13 06-27-2004 08:52 AM

Check out the NetBeans IDE, it comes with a layout called AbsoluteLayout. Lets you layout by pixel location.

SunGun 06-27-2004 08:30 PM

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

DigitalD17 07-09-2004 11:47 PM

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).

levity 07-26-2004 07:27 PM

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.


All times are GMT -8. The time now is 09:47 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54