user1748910
user1748910

Reputation: 105

How to stop JButton being enabled when mouse hovers over it?

I created a frame using NetBeans. The frame has two buttons A and B. Button A is initially disabled. It is to be enabled only when button B is clicked.

public newFrame() {    //newFrame is the name of the frame that has buttons A&B
    initComponents();

    btn_A.disable();
}

    private void btn_BActionPerformed(java.awt.event.ActionEvent evt)
{
    btn_A.enable();
}

The problem is that button A becomes active/enabled when the mouse is moved over it ie inspite of whether button B is clicked or not. How can i fix this?

I want button A to be enabled only after button B is clicked and not as a result of any other event.

Upvotes: 0

Views: 1364

Answers (3)

jatin3893
jatin3893

Reputation: 856

btn_A.enable() is a deprecated method.
To do this task, you could replace it by btn_A.setEnabled(false); to disable the button and btn_A.setEnabled(true); to enable the button.

Also, one more suggestion is, add statements like the following in your method if you feel something wrong happening:

    System.out.println("Some statement relevant to the method"); 

The main aim of those extra statements being you know when the method was actually executed.

Upvotes: 1

Sachin Mhetre
Sachin Mhetre

Reputation: 4543

Try the following code:

button. addMouseListener(new MouseAdapter() { 
      public void mouseEntered(MouseEvent me) { 
            button.setEnable(true); 
      } 

      public void mouseExited(MouseEvent me) { 
           button.setEnable(false); 
      } 

    }); 

Upvotes: 0

Extreme Coders
Extreme Coders

Reputation: 3511

Use btn_A.setEnabled(false) instead of btn_A.disable()

Upvotes: 1

Related Questions