10

I'm creating a Java GUI using Swing with Eclipse and Window Builder Pro. I'm using JButtons and JToggleButtons. I want to change toggle button's state from another button.

enter image description here

For example, when I click the clear grid, all the toggle buttons will be 'not selected'.

How can I do this? What are the methods that I have to use for toggle buttons and buttons?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ömer Faruk AK
  • 2,409
  • 5
  • 26
  • 47

4 Answers4

6

toggleButton.setSelected(boolean b)

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class JToggleButtonAction {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Selecting Toggle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JToggleButton toggleButton = new JToggleButton("Toggle Button");
        final JToggleButton toggleButton1 = new JToggleButton("Another Toggle Button");
        ActionListener actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
                boolean selected = abstractButton.getModel().isSelected();
                System.out.println("Action - selected=" + selected + "\n");
                toggleButton1.setSelected(selected);
            }
        };
        toggleButton.addActionListener(actionListener);
        frame.add(toggleButton, BorderLayout.NORTH);
        frame.add(toggleButton1, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mKorbel
  • 109,525
  • 20
  • 134
  • 319
5

Add actionListener to your JButton and in actionPerformed(ActionEvent) method change the state of all JToggleButtons. Make sure all your JToggleButton is accessible in this method. A simple example will be..

    JFrame frame = new JFrame("Panel image demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setLayout(new FlowLayout());

    final JToggleButton[] button = new JToggleButton[10];
    for (int i = 0; i < button.length; i++) {
        button[i] = new JToggleButton("Toggle Us");
        frame.add(button[i]);
    }
    JButton jButton = new JButton("Toggle that button");
    jButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (JToggleButton jToggleButton : button) {
                jToggleButton.setSelected(!jToggleButton.isSelected()); // <-- this will change the state of toggle button
            }
        }
    });

    frame.add(jButton);
    frame.setVisible(true);
Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
3

i was looking for this, this might help somebody out

B1.setSelected(false);

i made a method that make all my button false (unselect the toggles when i wanted it)

Sirko
  • 72,589
  • 19
  • 149
  • 183
3

Register an ActionListener to the JButton instance and make sure you can access the toggle buttons therein to manipulate their state.

mre
  • 43,520
  • 33
  • 120
  • 170