• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling method does not work from JMenuBar  RSS feed

 
Charlie Wall
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem with my application that is giving me headace. I hope someone can help me understand what is going on.

I have a paint program in Java where I can draw objects. The objects are stored in an arrayList. In the menubar the user can chooce "Back", which means the last item in the arrayList is removed. After that I want the program to loop through the arrayList and draw the remaining items.

My problem is that when I try to do that it will not work from the menu (menuItem2). If I instead add the code to one of the colorpanels (yellowPanel), from where the user can pick colors, it works fine.

menuItem2 uses ActionListener and yellowPanel uses MouseListener.




In class Draw:




In class FreeHand (class Rectangle looks the same except for it is using drawRec instead of drawLine)


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a lot of code. At a quick glance, you're drawing/painting in the event thread - that is generally not the right way to go about things. In the event thread you should adjust data structures to reflect the changes, and then call repaint(). The actual drawing code should be in the overridden paintComponent methods of the components that visualize your UI - usually a class extending JPanel which you would add to the content pane.

http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html goes into more detail on that.
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a printComponent method? The similarity to paintComponent will cause no end of confusion.
Yes, I see there is. Why have you given it static status? If you put all your drawing code in paintComponent you won't need printComponent anyway.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!