Change state of toggle button from another button
Asked Answered
E

4

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?

Endora answered 9/11, 2011 at 13:17 Comment(0)
B
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);
    }
}
Bethsaida answered 9/11, 2011 at 13:27 Comment(0)
P
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);
Propst answered 9/11, 2011 at 13:24 Comment(0)
B
3

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

Balladry answered 9/11, 2011 at 13:21 Comment(3)
Which function of toggle button is changes its state?Hadden
See setSelected(false)Balladry
The button state can be toggled using doClick() or set using setSelected(boolean). Check the javadocs for the effect of each.Lovins
H
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)

Homogenous answered 14/5, 2013 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.