How can I check that JButton is pressed? If the isEnable() is not work?
Solution 1
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");
Solution 2
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");
}
});
Solution 3
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
Solution 4
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");
}
Dexter Moregan
Updated on July 05, 2022Comments
-
Dexter Moregan almost 2 years
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.
- this code have 2 Jbuttons which are "Add" Button and "Checkout" button.
- the code will show the "Add button is pressed" message when I press "Checkout" button after I press "Add" button but If the "Add" Button is not pressed before the "Checkout" Button is pressed, the code will show the "Add Button is not pressed" message.
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?
-
MadProgrammer over 10 yearsNice suggestion, but unless the OP checks the state of the add button within it's
ActionListener
, it will always be disarmed/unpressed. This is howJButton
works... -
Sage over 10 yearsyes! In fact if he uses
ActionListner
, he don't even need to check against the isPressed type checking at all. He just could declare a boolean value and make it true when action event is listened by the action listener of the target button. I just wanted to let the user know these function againstisEnabled()
:). These days lots of user's question is not making much sense but others making reputation even not caring that: hence made me greedy. Apologies but who don't want to be rich ? -
Sage over 10 years+1: I have just commented about that though.. sorry didn't notice :) i do agree that he should change the design sense. In fact as he is checking with
Check button
whitherAdd Button
is pressed: i don't see any reason for depending on the input controlling component, as peeskiller has pointed out -
Sage over 10 yearsDepending on action command! No i don't see this as a good advice. Please re-think and read other answer :)
-
AJ. over 10 years@Sage you all are very good. But I tested this one and it works and it is a easy way.
-
MadProgrammer over 10 years@Sage No worries, the more people that say the same, the more likely we'll get our message across ;)
-
Dexter Moregan over 10 years@Sage still not work. the code give me "false" value although I pressed that button already Here the code: btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.out.println(""+btnAdd.getModel().isPressed() ); } });
-
Sage over 10 yearsFollow the answer MadProgrammer has suggested. He has already explained the reason why it won't work. My intention was to let you know that How the several state function exist to be working with. :)