• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with GridBagConstraint  RSS feed

 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • 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.



 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!