How can I check that JButton is pressed? I know that there is a method that its name is "isEnabled"
So I try to write a code to test.
Here the code:
final JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
panel.add(btnAdd);
JButton btnConfirm = new JButton("Check Out");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnAdd.isEnabled()) {
System.out.println("Add Button is pressed");
}
if (!btnAdd.isEnabled()) {
System.out.println("Add Button is not pressed");
}
}
});
When I run this code,the code give only the " Add button is pressed" although I didn't press the "Add" Button. Why does it occur like that?
JButton
has a model which answers these question:
isArmed()
, isPressed()
, isRollOVer()
etc. Hence you can ask the model for the answer you are seeking:
if(jButton1.getModel().isPressed())
System.out.println("the button is pressed");
JButton#isEnabled
changes the user interactivity of a component, that is, whether a user is able to interact with it (press it) or not.
When a JButton
is pressed, it fires a actionPerformed
event.
You are receiving Add button is pressed
when you press the confirm button because the add button is enabled. As stated, it has nothing to do with the pressed start of the button.
Based on you code, if you tried to check the "pressed" start of the add button within the confirm button's ActionListener
it would always be false, as the button will only be in the pressed state while the add button's ActionListener
s are being called.
Based on all this information, I would suggest you might want to consider using a JCheckBox
which you can then use JCheckBox#isSelected
to determine if it has being checked or not.
Take a closer look at How to Use Buttons for more details
Seems you need to use JToggleButton
:
JToggleButton tb = new JToggleButton("push me");
tb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JToggleButton btn = (JToggleButton) e.getSource();
btn.setText(btn.isSelected() ? "pushed" : "push me");
}
});
Just do System.out.println(e.getActionCommand());
inside actionPerformed(ActionEvent e)
function. This will tell you which command is just performed.
or
if(e.getActionCommand().equals("Add")){
System.out.println("Add button pressed");
}
The method you are trying to use checks if the button is active:
btnAdd.isEnabled()
When enabled, any component associated with this object is active and able to fire this object's actionPerformed method.
This method does not check if the button is pressed.
If i understand your question correctly, you want to disable your "Add" button after the user clicks "Check out".
Try disabling your button at start: btnAdd.setEnabled(false)
or after the user presses "Check out"
Use this Command
if(JButton.getModel().isArmed()){
//your code here.
//your code will be only executed if JButton is clicked.
}
Answer is short. But it worked for me. Use the variable name of your button instead of "JButton".
Source: Stackoverflow.com