• 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

Help with GridBagConstraint

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I am doing the 1st screen of my app and here is what I want to do. I have a JFrame that contains a small JPanel. The JPanel needs to be center of the screen.
The Panel consist of a JLabel, JTextField and JButton from top to bottom. When I add the panel to the frame, everything is perfect. However, I need to add a "CLOSE" button on the lower right hand corner of the screen. As soon as I add the button, the JPanel appear to be pushed to the left. Is there a way to add the close button without my JPanel get pushed to the left, please help.



 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't increase gridx, and set the anchor to the right east. Also set new insets.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Don't increase gridx, and set the anchor to the right east. Also set new insets.



Thanks Rob, it worked, here is my code for the button



I use a negative value for Insets, is that ok?
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember to reset the:
constraints.weighty = 0.0;
for the button.

Setting a negative inset is bad practice. You are setting 200 for the right for the panel and then -300 for the button. Instead, just set +100 for the panel and nothing for the button.
reply
    Bookmark Topic Watch Topic
  • New Topic