"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 JToggleButton
s 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
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);
}
}
}
}