Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



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

Repeating JPanels  RSS feed

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a more simplified version of the code I had for another question but I'm experieincing a different problem so I'm posting as the problem I'm having. When I try to resize the JApplet I have the child JPanel I want only once on the JApplet is actually repeating each time I drag the JApplet. How can I get it to only keep the one JPanel? Basically there should be one JPanel on the JApplet (right now anyways). I will be adding more once I've stepped through this code and resolved this issue.

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've read not to put in the JPanel paintComponent method but just put super.paintComponent(g) and that will clear the JPanel? If I just put that then it repeats objects added to the JApplet but if I put in this.removeAll it seems to work for the simple code I've got. If super.paintComponent(g) is supposed to clear why isn't it working above? Thanks for any help.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paintComponent() should be overridden only when you want to implement custom painting. Else leave it alone.
Add your child components outside the paint to fix the problem.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, Rob, if you insert this code into your post:

it then becomes an SSCCE. Maneesh's post should definitely be acted upon, but bear this in mind for future posts.

(Also by the way it's conventional for Java class names to start with a capital letter -- "TreeLayout". If you start your class name with a lower-case letter -- "treeLayout" -- then it looks like a variable name, which is off-putting to most Java programmers.)
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Paul. I noticed it before I had a chance to change it here (i.e. the JFrame bit), forgot about the 3rd class to be included here. Good point on the class name, missed that one.
Thanks for the response regarding paintComponent as well. I found a response to a similar question on another site whereby the guy gives explanation on paint() and paintComponent(). So I changed it and the first chunk of code I've re-implemented seems to work fine. There is a small issue with the initial display which I'll have to look into though but other than that first bit it's looking okay.
Thanks again for the responses as well. Most helpful. Appreciate it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!