0

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//
    }
}

}
brain storm
  • 30,124
  • 69
  • 225
  • 393
  • 4
    Why not simply put the buttons, or perhaps better their AbstractActions, in a collection, and give it a `setEnabled(boolean enabled)` method. In the method iterate through the collection setting the enabled property of each button or Action. – Hovercraft Full Of Eels Oct 22 '13 at 23:39
  • sorry, I dont understand what you mean here – brain storm Oct 22 '13 at 23:42
  • 1
    Simply put your buttons in an `ArrayList`. Give the class that holds the buttons and this list a method to set the state of the buttons. In that method iterate through the list. It's nothing but basic nuts and bolts Java. Surely you've used array lists and looped through them with a for loop, right? – Hovercraft Full Of Eels Oct 22 '13 at 23:44
  • I think I undestood now. let me implement and see how it goes. Thanks for the input – brain storm Oct 22 '13 at 23:50
  • @HovercraftFullOfEels: can you kindly look at this post, http://stackoverflow.com/questions/19555075/how-to-implement-mvc-pattern-for-word-guessing-game – brain storm Oct 24 '13 at 01:22

2 Answers2

3

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);
        }
    }
Tim Boudreau
  • 1,741
  • 11
  • 13
  • +1, for not using a List to store the buttons. You can just as easily get the buttons directly from the container. – camickr Oct 22 '13 at 23:58
1

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...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • why do you have `super.setEnabled(enabled);`? – brain storm Oct 23 '13 at 01:31
  • @user1988876 It's an obligation. If I recall correctly, `setEnabled` will fire a property change event, so there might be parts of the system that might like to respond to it. Blindly disregarding implemented functionality is a sure way to destroying your application ;) – MadProgrammer Oct 23 '13 at 01:39
  • `so there might be parts of the system that might like to respond to it`..what do you mean here? – brain storm Oct 23 '13 at 02:24
  • `setEnabled` fires a property change event. There may be other parts of the application or framework which monitor these changes and would like to respond to the change. – MadProgrammer Oct 23 '13 at 02:28
  • I have a couple of questions while implementing this? how can I reach you? chat forum over here? Thanks! – brain storm Oct 23 '13 at 02:36
  • Try [here](http://chat.stackoverflow.com/rooms/39787/enableddisablemultiplebuttons) – MadProgrammer Oct 23 '13 at 02:40
  • @camickr Never an issue with keeping the code simple and you might be right in this context, but if the layout becomes more complex, spanning multiple containers for example, it would simpler to maintain a list. This is also a "general" approach to the problem – MadProgrammer Oct 23 '13 at 03:12