Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calling repaint() doesn't seem to work  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4585
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an inner class that extends JPanel so i can override getPreferredSize(). in the example below i have a button. in actionPerformed() i call the repaint() method of this inner class. nothing happens until i type something. then getPreferredSize() gets called. if you want to check it out, try changing the paper size to A3.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple answer: you need a revalidate() before the repaint().

Better answer: A Swing component should be responsible for repainting itself whenever one of its properties change. Your application code should NOT be invoking repaint().

So, the width/height should be properties of the TextPanel, not of the Example class. In your case the width/height is based on the current PageFormat so you would probably want a method like "setPageFormat()". Then your getPreferredSize() method would query the width/height of the PageFormat to return the appropriate size.

Something like:



The revalidate() is used to invoke the layout manager so that the components size and location can be reset based on the property change. Then the repaint() is done to paint the component at its new size/location.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!