This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Change location of component using grid bag layout  RSS feed

 
Chaz Branham
Ranch Hand
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day all!

Can anyone explain or point me in the right direction of how to change the location of a grid bag positioned component after it's already been added to the layout earlier?

Example is if I want component 1 to change from row 1 to row 2.

Thank you for your help and time,
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me that sounds like the hard way to do things. I would just redraw the panel with the components in their new places. Of course I know nothing about your components so I can't tell whether it would be possible to swap their contents.
 
Campbell Ritchie
Sheriff
Posts: 55324
157
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually discuss such things in our GUIs forum, so I shall move this thread.

I would suggest you probably should not move Components after the GUI has become visible. That would give a very peculiar appearance. There are various ways of changing appearance. Maybe a card layout???
 
Brett Spell
Ranch Hand
Posts: 118
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaz Branham wrote:
Can anyone explain or point me in the right direction of how to change the location of a grid bag positioned component after it's already been added to the layout earlier?

Example is if I want component 1 to change from row 1 to row 2.


Construct an instance of GridBagConstraints with the new constraint values you want to use and call the GridBagLayout's setConstraints() method with those constraints and the component you want to move / update the constraints.

For it to actually change the component's position you'll need the layout manager to re-layout the components, though, and if I recall correctly (it's not often necessary or appropriate to trigger this "manually") the right way to accomplish that is call the Container's doLayout() method.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the right way to accomplish that is call the Container's doLayout() method.


That is not the correct way to do that. Read the API description of the method and it tells you the proper method to invoke.

Having said that, I usually use the revalidate() method since this is a Swing app and not AWT.
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:[That is not the correct way to do that. Read the API description of the method . . .


You're right. As I said, I haven't often found a need to "manually" refresh a layout, which is why I qualified my response.
 
Chaz Branham
Ranch Hand
Posts: 53
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the replies. I was able to achive what I wanted by removing all of the components and re-adding them back to the same JPanel then calling revalidate() made it work very nicely. I will experiment with the setconstraints as that sounds like a cleaner way of doing this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!