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

前端 未结 6 1525
Happy的楠姐
Happy的楠姐 2020-11-30 14:22

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
相关标签:
6条回答
  • 2020-11-30 14:44

    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");
    }
    
    0 讨论(0)
  • 2020-11-30 14:44

    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"

    0 讨论(0)
  • 2020-11-30 14:47

    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");
        }
    });
    
    0 讨论(0)
  • 2020-11-30 14:50

    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");
    
    0 讨论(0)
  • 2020-11-30 14:57

    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

    0 讨论(0)
  • 2020-11-30 14:58

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

    0 讨论(0)
提交回复
热议问题