Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 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:     [                      ]
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 08:29 PM..
SunGun is offline  
Old 06-17-2004, 10:35 AM   #2 (permalink)
Upright
 
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  
Old 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
sargorn is offline  
Old 06-26-2004, 08:47 PM   #4 (permalink)
Fluxing wildly...
 
MrFlux's Avatar
 
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  
Old 06-27-2004, 08:52 AM   #5 (permalink)
Upright
 
Check out the NetBeans IDE, it comes with a layout called AbsoluteLayout. Lets you layout by pixel location.
bogosj13 is offline  
Old 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.
SunGun is offline  
Old 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).
DigitalD17 is offline  
Old 07-26-2004, 07:27 PM   #8 (permalink)
Upright
 
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  
 

Tags
form, gui, ick, java, laying


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



All times are GMT -8. The time now is 11:52 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360