This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use ItemStateChanged()?

 
garconcn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My simple Japplet program has some problems, I don't know how to use ItemStateChanged(). Where should I change in my program?
Thank you for any comments.


Garconcn

�The user can specify the number of rectangles to be drawn.
�The user will also specify the desired color.


 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are using JMenuItems, I would use ActionListener, not ItemStateListener. JMenuItems don't have any sense of state - you just click on them. If you want items you can check on or off, just JCheckBoxMenuItem. Anyway, assuming you decide to use ActionListener instead:

1) Implement ActionListener instead of ItemListener
2) Use RedI.addActionListener(this) instead of addItemListener
3) Create a method called public void actionPerformed(ActionEvent e)

Now, when you click on a menu item that has an ActionListener added, the actionPerformed method will be called. So inside the actionPerformed method you should put code to determine which item was selected. You actually have most of the code to do this already, you just put it in the paint method by mistake.

Once you have done that you will find a couple more mistakes - but don't be discouraged! You actually have made a pretty good attempt, which is more than some people do when they post their requests here!
 
garconcn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stuart Gray:
Since you are using JMenuItems, I would use ActionListener, not ItemStateListener. JMenuItems don't have any sense of state - you just click on them. If you want items you can check on or off, just JCheckBoxMenuItem. Anyway, assuming you decide to use ActionListener instead:

1) Implement ActionListener instead of ItemListener
2) Use RedI.addActionListener(this) instead of addItemListener
3) Create a method called public void actionPerformed(ActionEvent e)

Now, when you click on a menu item that has an ActionListener added, the actionPerformed method will be called. So inside the actionPerformed method you should put code to determine which item was selected. You actually have most of the code to do this already, you just put it in the paint method by mistake.

Once you have done that you will find a couple more mistakes - but don't be discouraged! You actually have made a pretty good attempt, which is more than some people do when they post their requests here!



Your explanation is very clear, now I understand how actionPerformed method works. I will try to fix it.
Thank you for your explanation and encouragement.

Garconcn
[ June 04, 2005: Message edited by: garconcn ]
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic