We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Implementing Action Listener and Change Listener

Hello,

I want to implement Change Listener in the Action Listener of a Button

I have a color graph chooser and 2 buttons...Apply and Cancel

to select the color of the color chooser i have implemented the Change Listener
but I want the change listener to act on the Apply button click...

Since the listener which I have written is not in the apply button it takes place even when I click on cancel button....

code for change listener

final JColorChooser ColorChooser = new JColorChooser(Color.black);
   ColorChooser.getSelectionModel().addChangeListener(new ChangeListener()
   {
     public void stateChanged(ChangeEvent e)
      {
         newColor = ColorChooser.getColor();
      }
     }
   );

code for action listener

public class ButtonHandler implements ActionListener
 {
   public void actionPerformed(ActionEvent ae)
    {
      Object button =ae.getSource();
      if(button==Apply)
       {
          setVisible(false);
       }
      if(button==Cancel)
      {
        setVisible(false);
       }
     }
  }//event
3
Contributors
2
Replies
5 Hours
Discussion Span
8 Months Ago
Last Updated
3
Views
Question
Answered
poojavb
Posting Pro
524 posts since Nov 2011
Reputation Points: 39
Solved Threads: 77
Skill Endorsements: 5

doesn't make any sense to me. just drop the ChangeListener and add all the logic that is being called in it's method in the actionPerformed method of the ActionListener.

stultuske
Industrious Poster
4,374 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
Question Answered as of 8 Months Ago by stultuske
public class ButtonHandler implements ActionListener
 {
   public void actionPerformed(ActionEvent ae)
    {
      Object button =ae.getSource();
      if(button==Apply)
       {
             newColor = ColorChooser.getColor();
          setVisible(false);
       }
      if(button==Cancel)
      {
        setVisible(false);
       }
     }
  }//event
poojavb
Posting Pro
524 posts since Nov 2011
Reputation Points: 39
Solved Threads: 77
Skill Endorsements: 5

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0666 seconds using 2.74MB