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

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.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.