Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

setEnabled(false) does not work as expected

801424Jul 19 2010 — edited Jul 21 2010
I have created the simple application below. A JFrame with a single button. When the button is clicked the line
jButton1.setEnabled(false);
should disable the button.
The button does get shaded out but continues to work with the System.out.println("Mouse Pressed"); line still printing the message to the screen when the button is pressed.
Please tell me why this happens?
package editable;


public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        this.setBounds(20, 20, 400, 400);
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1MousePressed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(131, 112, 73, 23);

        pack();
    }// </editor-fold>

    private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
     
        jButton1.setEnabled(false);
        System.out.println("Mouse Pressed");
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

        NewJFrame njf = new NewJFrame();
njf.setVisible(true);

    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 18 2010
Added on Jul 19 2010
3 comments
955 views