• 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

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
reply
    Bookmark Topic Watch Topic
  • New Topic