Question

There are 12 JButtons in a JPanel. I want to click in one of them and disable all the others (It will be "highlighted"). And then, if I click a disabled one, then the previous gets disabled and the process is the same. I want to do this using iteration and a custom method, considering that there are 12 buttons and I want to avoid repeating code.

Consider that the 12 JButtons are already declared. Then we create an ArrayList of JButtons to store them and an ArrayList of booleans to store their boolean-enabled-values:

ArrayList<JButton> arrayOfButtons = new ArrayList<JButton>();
ArrayList<Boolean> arrayOfBooleans = new ArrayList<Boolean>();

We add all the buttons to the arrayOfButtons:

arrayOfButtons.add(btn1);
arrayOfButtons.add(btn2);
arrayOfButtons.add(btn3);
arrayOfButtons.add(btn4);
arrayOfButtons.add(btn5);
arrayOfButtons.add(btn6);
arrayOfButtons.add(btn7);
arrayOfButtons.add(btn8);
arrayOfButtons.add(btn9);
arrayOfButtons.add(btn10);
arrayOfButtons.add(btn11);
arrayOfButtons.add(btn12);

And then we store in the arrayOfBooleans their enabled states by iterating over arrayOfButtons:

for (int i = 0; i < arrayOfButtons.size(); i++) {
    arrayOfBooleans.add(arrayOfButtons.get(i).isEnabled());
}

Now, how can I implement a MouseListener and create the enabling-by-exclusion system by iterating over arrayOfButtons? The idea is pretty simples: "Enable one, disable the others.", but I can't figure out how to do it in practice.

I thank you very much!

Was it helpful?

Solution

"And then, if I click a disabled one, then the previous gets disable and the process is the same" doesn't make sense. If the button is disabled it will not capable of reacting to input from the user.

While the context is a little light, why not use JToggleButtons instead

Take a look at How to use buttons for some more details

This would also mean you could group all the buttons into a ButtonGroup which would take care of making sure that only one button was "active" at a time

Updated with simple example

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ToggleButtons {

    public static void main(String[] args) {
        new ToggleButtons();
    }

    public ToggleButtons() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<JToggleButton> listOfButtons;
        private ButtonGroup commonButtonGroup;

        public TestPane() {
            commonButtonGroup = new ButtonGroup();
            listOfButtons = new ArrayList<>(25);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            for (int index = 0; index < 10; index++) {
                JToggleButton tb = new JToggleButton(Integer.toString(index));
                commonButtonGroup.add(tb);
                listOfButtons.add(tb);
                add(tb, gbc);
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top