1

I have the following code from this link.. It uses Swing framework and a simple ChangeListener example.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class StateListener {

    public static void main(String args[]) {

  JFrame jFrame = new JFrame("");

  jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JButton button = new JButton("Press Me");

  ActionListener actionListner = new ActionListener() {

@Override

public void actionPerformed(ActionEvent event) {

    AbstractButton absButton = (AbstractButton) event.getSource();

    boolean selected = absButton.getModel().isSelected();

    System.out.println("Selected=" + selected + " \n");

}

  };

  ChangeListener changeListner = new ChangeListener() {

@Override

public void stateChanged(ChangeEvent event) {

    AbstractButton aButton = (AbstractButton) event.getSource();

    ButtonModel aModel = aButton.getModel();

    boolean armed = aModel.isArmed();

    boolean pressed = aModel.isPressed();

    boolean selected = aModel.isSelected();

    System.out.println("Armed :" + armed + " - Pressed :" + pressed + " - Selected :" + selected);

}

  };

  button.addActionListener(actionListner);

  button.addChangeListener(changeListner);

  Container cPane = jFrame.getContentPane();

  cPane.add(button, BorderLayout.CENTER);

  jFrame.setSize(800, 500);

  jFrame.setVisible(true);
    }
}

my questions are : 1) when I click the Press Me, the output is

Armed :true - Pressed :false - Selected :false
Armed :true - Pressed :true - Selected :false
Selected=false 

Armed :true - Pressed :false - Selected :false
Armed :false - Pressed :false - Selected :false

I have only two calls to Listener at lines 61 and 63 which are.

button.addActionListener(actionListner);

  button.addChangeListener(changeListner);

The output should be

Selected=false 

    Armed :true - Pressed :false - Selected :false

but I am getting 4 lines of output, which I am unable to understand

2). Are the methods, addActionListener and addChangeListener part of JButton Class since they are not overridden by ChangeListener or ActionListener?

Thanks, I am a newbie to using Swing Framework. Any help is much appreciated.

brain storm
  • 30,124
  • 69
  • 225
  • 393

2 Answers2

3

When a mousePressed event is received on a button the following code is executed:

if (!model.isArmed()) 
{
    model.setArmed(true);
}
model.setPressed(true);

Every time the state of the model is changed a ChangeEvent is generated to you get two events.

Same on a mouseReleased:

model.setPressed(false);
model.setArmed(false);

So each MouseEvent will generate multiple ChangeEvents.

Are the methods, addActionListener and addChangeListener part of JButton

Read the API documentation. It will tell you in which class the methods are defined.

The output should be

The order of output is not dependent on the order in which you add the listeners to the component. The ActionEvent will not be generated until the mouse have been pressed and released or the spacebar has been pressed and released.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • how do I know what occurs when event is triggered? for example, I have 5 lines of output, would it be possible to tell which events are triggered. eg , if you could relate the first output to mouse press, and second output to mouse release and so on. I try to follow with what you gave here, but unable to fill in the gaps for all 4 lines of ouput. some help? – brain storm Sep 09 '13 at 18:57
  • You do a mouse pressed and hold it for a few seconds to see what output is displayed. Then you release the mouse and see what happens. Also I gave you the exact code so you can follow what ChangeEvent is generated when the model method is invoked. `some help?` - what help more do you need? – camickr Sep 09 '13 at 20:11
  • Thanks, by keeping the mouse down and holding it for a few seconds, I could see two events being caused.If my understanding is correct, each time the state is changed, for ex, `model.setArmed(true)` is executed, it changes the state and hence `stateChanged method` is invoked. – brain storm Sep 09 '13 at 20:32
  • @user1988876 [this code will help you (see Borders)](http://stackoverflow.com/a/5755124/714968) – mKorbel Sep 09 '13 at 20:39
2
boolean selected = absButton.getModel().isSelected();

should be only

boolean selected = absButton.getModel().isPressed();

only ChangeListener can firing an events from ButtonModel and its Mouse and Key Events implemented in XxxButtonUI,

isArmed();
isPressed();
isSelected();
isRollover() 

ActionListener is fired from mouseClicked and ENTER and SPACE Key Events, those KeyBindings are implemented in JButtons API

mKorbel
  • 109,525
  • 20
  • 134
  • 319