Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change state of toggle button from another button

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?

like image 399
Ömer Faruk AK Avatar asked Nov 09 '11 13:11

Ömer Faruk AK


People also ask

How do I change the text button on toggle?

toggleButton. setTextOn(textOn); // Sets the text for when the button is in the checked state. To set the text using xml, use the following: android:textOff="The text for the button when it is not checked." android:textOn="The text for the button when it is checked."

How do I toggle a button in HTML?

We can do that by using the HTML label tag and HTML input type = checkbox. HTML code: The HTML code is used to create a structure of toggle switch. Since it does not contain CSS so it is just a simple structure.

How do I use the toggle button on KIVY?

The ToggleButton widget acts like a checkbox. When you touch or click it, the state toggles between 'normal' and 'down' (as opposed to a Button that is only 'down' as long as it is pressed). Only one of the buttons can be 'down'/checked at the same time.


2 Answers

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);
    }
}
like image 64
mKorbel Avatar answered Oct 24 '22 05:10

mKorbel


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);
like image 5
Harry Joy Avatar answered Oct 24 '22 04:10

Harry Joy