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

Revalidate() is resetting a JTextArea's position on JPanel  RSS feed

 
Gregory Miller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I have a program that allows me to dynamically add JTextAreas and then drag them around the screen. The problem is, when I add a new JTextArea, I call revlidate() to update the panel. This causes all of the JTextAreas to reset their positions. How can I prevent this from happening? It is happening on this code:





Any ideas? Thanks!

 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The revalidate() method invokes the layout manager and the layout manager will determine the size/location of the component.

If you really need random placement of your components then you need to use a "null layout". Now you will be responsible for setting the size/location of the component when you add it to the panel.

However a better solution (as you already know for this posting: http://stackoverflow.com/questions/26705798/dynamically-adding-a-jtextarea-to-jpanel-not-working is do use a custom layout) You may want to check out Drag Layout which handles this for you.
 
Gregory Miller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:The revalidate() method invokes the layout manager and the layout manager will determine the size/location of the component.

If you really need random placement of your components then you need to use a "null layout". Now you will be responsible for setting the size/location of the component when you add it to the panel.

However a better solution (as you already know for this posting: http://stackoverflow.com/questions/26705798/dynamically-adding-a-jtextarea-to-jpanel-not-working is do use a custom layout) You may want to check out Drag Layout which handles this for you.


I'm not sure what you mean "random". I will be allowing the user to drag the boxes around.

Your link helps a lot, but I'm still fuzzy on how a LayoutManager does this. Where exactly in the DragLayout is it fixing the revalidate() problem that I am having?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally a layout manager sets the size/location of a component (among other things). The DragLayout doesn't set the location. It respects that the component can be dragged around the panel, so it just sets the size of the component. So when you invoke revalidate(), the layout manager is called and the size of the component is set (if required) but the location is not changed.

Read the Swing tutorial on Layout Managers for more information about how layout managers work.
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!