Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Calling method does not work from JMenuBar

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)

Posts: 43016
  • 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. goes into more detail on that.
Posts: 70305
  • 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.
    Bookmark Topic Watch Topic
  • New Topic