0

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);
    }

  • And you are using (or learning) applets because .... ? Applets were deprecated a long time ago. Web browsers no longer support them. Java (9 onwards) is progressively removing support. Writing new applet code (or learning how to do it) is nugatory. – Stephen C Jun 07 '23 at 23:10
  • See also https://stackoverflow.com/questions/45535112 – Stephen C Jun 07 '23 at 23:14
  • Coz I'm working on a legacy code originally written in 2002. It's research work, so got to stick to the interface. @StephenC – Vedant Bahel Jun 08 '23 at 02:58
  • @StephenC [Everything old is new again!](https://www.webswing.org/docs/23.1/configure/applet.html) – Elliott Frisch Jun 08 '23 at 15:46

2 Answers2

0

This

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

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

this.closeButton = new JButton(" Close Hint ");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks, that worked. I also want to do this for another button. I tried implementing the same thing but that didn't work. This is the code for the other button: openExplanationPanel = new JPanel(); openExplanationPanel.setAlignmentX(0.0f); JButton openExplanationButton = new JButton( isLevel1HintExplanation ? Content.getLevel1Question() : Content.getLevel1QuestionAgain()); – Vedant Bahel Jun 08 '23 at 02:56
  • Why are you creating another panel? `buttonsPanel.add(openExplanationButton, BorderLayout.EAST);` should do it. Maybe. – Elliott Frisch Jun 08 '23 at 03:13
  • Cool. It could make it work. Thanks a lot! – Vedant Bahel Jun 08 '23 at 03:31
0

Access to variable issue

By declaring closeButton variable outside of initUI you now make it possible to access this variable from the openExplanation() method.

Code:

// Declare JButton closeButton variable here
private JButton closeButton;

private void initUI() {

  hintArea = new JDialog(this);

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

  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);
}
AztecCodes
  • 1,130
  • 7
  • 23