Question

I have this code snippet (a part of code actually) where in one class I have all the Jbutton created for 26 alphabets. I have another class that keeps track of time and when the time is over or the game is over, I want to disable all the 26 JButtons in one shot.

here is the code for creating Jbuttons

public class DetailsPanel extends JPanel {



        public DetailsPanel() {
            setLayout(new BorderLayout());
            setBorder(BorderFactory.createTitledBorder(" click here "));

            JPanel letterPanel = new JPanel(new GridLayout(0, 5));
            for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
                String buttonText = String.valueOf(alphabet);
                JButton letterButton = new JButton(buttonText);
                letterButton.addActionListener(clickedbutton());
                letterPanel.add(letterButton, BorderLayout.CENTER);
            }
            add(letterPanel, BorderLayout.CENTER);
        }

        }

In my maincontrol class, I want to turn off all the Jbuttons like in

public class maincontrol {
     int counter;
     DetailsPanel dp;
     public maincontrol(DetailsPanel dp) {
     this.dp = dp;
     int counter = 0;
}

public void turnoff(){
   if ( counter>10){
       //turn off all here//
    }
}

}
Was it helpful?

Solution

Keep a reference to your DetailsPanel. Add a method to it to disable the buttons, such as:

public class DetailsPanel extends JPanel {
    private final JPanel letterPanel;
    public DetailsPanel() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder(" click here "));

        letterPanel = new JPanel(new GridLayout(0, 5));
        ...
    }
    public void disableButtons() {
        for (Component c : letterPanel.getComponents()) {
          if (c instanceof JButton) c.setEnabled(false);
        }
    }
}

call it when you want to disable the buttons. Or be a little more clever, and do it based on the number of turns internally, which you pass in:

    private static final int MAX_TURNS = 10;
    public void updateButtons(int turn) {
        for (Component c : letterPanel.getComponents()) {
          if (c instanceof JButton) c.setEnabled(turn <= MAX_TURNS);
        }
    }

OTHER TIPS

As Hovercraft Full Of Eels has already suggested, you could simply keep all the buttons in a simple java.util.List of some kind and simply iterate through the list, changing the state of the buttons when you need to...

For example...

public class DetailsPanel extends JPanel {

    private List<JButton> buttons = new ArrayList<>(26);

    public DetailsPanel() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder(" click here "));

        JPanel letterPanel = new JPanel(new GridLayout(0, 5));
        for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
            String buttonText = String.valueOf(alphabet);
            JButton letterButton = new JButton(buttonText);
            buttons.add(letterButton);
            letterButton.addActionListener(clickedbutton());
            letterPanel.add(letterButton, BorderLayout.CENTER);
        }
        add(letterPanel, BorderLayout.CENTER);
    }

    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (JButton btn : buttons) {
            btn.setEnabled(enabled);
        }
    }

}

This would mean, when you want to disable the buttons, you would simply do something like...

// detailsPane is a reference to an instance of DetailsPane
detailsPane.setEnabled(false);

And

// detailsPane is a reference to an instance of DetailsPane
detailsPane.setEnabled(true);

When you want to enable them...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top