This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to use ItemStateChanged()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to use ItemStateChanged()?" Watch "How to use ItemStateChanged()?" New topic
Author

How to use ItemStateChanged()?

garconcn
Greenhorn

Joined: May 26, 2005
Posts: 2
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

Joined: Apr 21, 2005
Posts: 410
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

Joined: May 26, 2005
Posts: 2
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

Joined: Apr 21, 2005
Posts: 410
You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use ItemStateChanged()?
 
Similar Threads
Why does it not show the new coordinates?!!??!!
Ladders and slides
Too many ActionEvents after button press.
Need help writing a code involving bar charts!
Weird problem in Windows 7 and Ubuntu 10.10 in a java application