How can I check that JButton is pressed? If the isEnable() is not work?
Asked Answered
C

6

6

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.

  1. this code have 2 Jbuttons which are "Add" Button and "Checkout" button.
  2. 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?

Classics answered 27/12, 2013 at 10:35 Comment(8)
after initializing btnAdd.setEnabled(false);Fricandeau
What exactly is the purpose of checking if it is pressed? The listener is registerd to only one button, so Just perform the action desired without checking anythingSalinometer
@peeskillet how to checkout without adding anything !!Fricandeau
@iShaalan, what do you mean?Salinometer
If you're trying to check if a different button has previously been pressed (for the pupose of checking if any items have been added), you're going about this all wrong. Instead you should check if some list of item is empty or not.Salinometer
@peeskillet exactly , What if it is removed ? :))Fricandeau
@peeskillet, the OP actually wants a boolean check whither the Add Button is pressed before Check button. But i think you got the nail: the OP should not depend on such functionality on the controlling component which are to take input data rather on the data container itself whither their is required data exist or notPoche
So what is it you're actually trying to accomplish with the if statement, besides checking if another button is pressedSalinometer
P
18

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");
Poche answered 27/12, 2013 at 10:41 Comment(4)
Nice suggestion, but unless the OP checks the state of the add button within it's ActionListener, it will always be disarmed/unpressed. This is how JButton works...Asparagine
yes! 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 against isEnabled() :). 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 ?Poche
@Poche 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() ); } });Classics
Follow 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. :)Poche
E
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");
    }
});
Esoterica answered 27/12, 2013 at 10:42 Comment(0)
A
2

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 ActionListeners 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

Asparagine answered 27/12, 2013 at 10:51 Comment(2)
+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 whither Add Button is pressed: i don't see any reason for depending on the input controlling component, as peeskiller has pointed outPoche
@Poche No worries, the more people that say the same, the more likely we'll get our message across ;)Asparagine
P
1

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");
}
Phototelegraphy answered 27/12, 2013 at 10:53 Comment(2)
Depending on action command! No i don't see this as a good advice. Please re-think and read other answer :)Poche
@Poche you all are very good. But I tested this one and it works and it is a easy way.Phototelegraphy
K
0

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"

Kerk answered 27/12, 2013 at 11:4 Comment(0)
T
0

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".

Trust answered 26/4, 2020 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.