• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GridBagConstraints help

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want all the buttons to be at the top of the screen.
I thought I had the code right, but the buttons are still being displayed in the centre of the screen.
How do I get them to the top?


Thanks,
Charlie
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the API for GridBagConstraints weighty and fill attributes. Essentially you need to add an invisible "filler" component after you have added all your buttons and tell the GridBagConstraints that it will occupy all of the remainder space so it "pushes" the buttons up. I usually use an empty JLabel as a filler.

Try out your code with these additions just before the call to set visible.


Some pointers.
1) Instead of frame.setLayout and frame.add, you should be performing these operations on the content pane. Check out JFrame#getContentPane()
2) I personally prefer nested panels. getContentPane defaults to BorderLayout which I let remain. I would use a JPanel, set it to GridBagLayout, add the buttons and then add the panel to the content pane. This gives me the flexibility to add something else to the content pane North, South, East, West in the future if required. This might sound a few more lines of coding, but I find it worth it, based on my experience. But then this is a personal preference.
3) Instead of explicitly defining c.gridx=1, try to use the incremental operator. Notice c.gridy++ in my code. 1 defines an absolute value whereas ++ defines an incremental value. The advantage is that you do not need to touch any other code in case you insert/delete some other component in the future.

PS. It is nice to see someone "brave" enough to take on the GridBagLayout . If you google, you will find why most developers shy away from it
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in java how to program 6th edition by deitel and deitel there is a great example for gridbaglayout...


also refer to the www.java2s.com
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasannakumar,
The OPs problem is not how to use a GridBagLayout but how to achieve a specific desired effect.
 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked at the code you wrote Maneesh and I understand where I was going wrong now, thank you.

How would I add something else (like a table) to fill the rest of the space, rather than the empty JLabel you used in your example?
I tried the following but it's not working.



Thanks,
Charlie
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just another cross poster.
http://forums.sun.com/thread.jspa?threadID=5417064
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Just another cross poster.
http://forums.sun.com/thread.jspa?threadID=5417064


And one of those who doesn't bother to tell the folks here that the problem has been solved on the other forum.
reply
    Bookmark Topic Watch Topic
  • New Topic