IsEnabled not working when a function is triggered in Java applet

61 Views Asked by At

I want a button ("closeButton") to grey out when the function "openExplanations" run. But it is not working.

Gives error Cannot invoke "javax.swing.JButton.setEnabled(boolean)" because "this.closeButton" is null


//the UI
private void initUI() {

hintArea = new JDialog(this);

buttonsPanel = new JPanel();
buttonsPanel.setAlignmentX(0.0f);

JButton closeButton = new JButton(" Close Hint ");
        closeButton.setActionCommand("closeHint");
        closeButton.setVisible(true);
        closeButton.addActionListener(this);
        closeButton.setFocusable(false);
        closeButton.setIcon(createImageIcon("images/constraint/close.gif"));
        buttonsPanel.add(closeButton, BorderLayout.WEST);

hintArea.add(buttonsPanel, BorderLayout.CENTER);
}


private void openExplanation(){
        closeButton.setEnabled(false);
    }

1

There are 1 best solutions below

3
Elliott Frisch On

This

JButton closeButton = new JButton(" Close Hint ");

creates a variable shadow. Remove the type declaration. And use this.

this.closeButton = new JButton(" Close Hint ");