Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I stop all of my insets from changing?  RSS feed

 
John Ironmonger
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using the GridBagLayout to try and position my buttons surrounding the center of my frame, with a gap in the middle. I'm trying to do this using insets, to position each different component where I want it. However, whenever I change, say the 4th set of insets, the GridBagLayout adopts these new insets for all of my components, instead of just using it for that one component. I'm declaring new insets for each button so I don't understand why it just uses the last one ? Thanks for any help.

Here's the class to run the panel which the buttons are on :



Here's the panel with the problematic buttons and insets :

 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't call a constraints object gbLayout; that is very confusing. Call it gbCons or similar.
You appear to be changing the insets, but not the location of the buttons; you need to set positions and widths as well as the insets. Yes, this does become very time‑consuming, so you should google for Cai Horstmann's GBC class; a GBC object makes setting the constraints much easier.
When your Insets are as large as they are, you will never get them to work; they extend well across the entire display. You are supposed to use something like new Insets(5, 5, 5, 5).
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted doesn't even compile so I'm not going to guess what is wrong. First you should learn some basics:

1. Variable names should NOT start with an upper case character.
2. You should NOT be extending JComponent. You would extend JComponent when you want to create a custom component and do custom painting.
3. Since all you want to do is add components to a panel then you should be extending JPanel and then in the constructor of your class you would create your buttons and add them to the panel.
4. Use the proper add(...) method. The constraint should be the second parameter and the panel should be added to the frame BEFORE the frame is visible.

As a quick summary the basic code to create the frame should be:



If you fix the above and still have problems and you post the better structured code, then maybe I can have a look at it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!