1

I have a questions about using array I have a method that creates JButton for a JPanel. I used JButton[] to store 7 buttons in it.

The problem is those buttons must be setEnable(false) initially. they are only enabled when openButton is clicked.

I would like to create another method that just take all elements from JButton[] and set them Enable. How can I do that?

private JButton filterButtons() {
    //set layout for buttons
    setLayout(new BorderLayout());

    //Array to store description of buttons
    final String[] filterNames = {myEdgeDetect.getDescription(), 
                                    myEdgeHighlight.getDescription(),
                                    myFlipHorizontal.getDescription(),
                                    myFlipVeritcal.getDescription(),
                                    myGrayscale.getDescription(),
                                    mySharpen.getDescription(),
                                    mySoften.getDescription()};

    final JButton[] filterButtons = new JButton[filterNames.length];

    //This panel store buttons on north
    final JPanel buttonPanel = new JPanel();

    //Start to print buttons
    for (int i = 0; i < filterNames.length; i++) {
        filterButtons[i] = new JButton(filterNames[i]);
        filterButtons[i].setEnabled(false);
        filterButtons[i].addActionListener(null);
        filterButtons[i].setActionCommand(" " + i);
        buttonPanel.add(filterButtons[i]);

    }
Joe
  • 11
  • 4

1 Answers1

1

If, rather than defining filterNames and filterButtons inside the method you made them fields of the class, then it would be easy to write a second method that iterates through the filterButtons and enables them.

Class Whatever {
    String[] filterNames;
    JButton[] filterButtons;

    private JButton filterButtons() {
      //set layout for buttons
      setLayout(new BorderLayout());

      //Array to store description of buttons
      filterNames = {myEdgeDetect.getDescription(), 
                                    myEdgeHighlight.getDescription(),
                                    myFlipHorizontal.getDescription(),
                                    myFlipVeritcal.getDescription(),
                                    myGrayscale.getDescription(),
                                    mySharpen.getDescription(),
                                    mySoften.getDescription()};

      filterButtons = new JButton[filterNames.length];

      //This panel store buttons on north
      final JPanel buttonPanel = new JPanel();

      //Start to print buttons
      for (int i = 0; i < filterNames.length; i++) {
        filterButtons[i] = new JButton(filterNames[i]);
        filterButtons[i].setEnabled(false);
        filterButtons[i].addActionListener(null);
        filterButtons[i].setActionCommand(" " + i);
        buttonPanel.add(filterButtons[i]);

      }
   }

   void enableButtons() {
    for (int i = 0; i < filterButtons.length; i++) {
        filterButtons[i].setEnabled(true);
    }
  }
}
Evan Williams
  • 406
  • 2
  • 8