Identify selection of Jradiobutton

Asked

Viewed 398 times

0

I created a code in java with 2 Jradiobuttons and need it to appear on the console which radio is selected, and when change radio, appear on the console as well. I’ve tried using the if(rad1.isSelected()){"Argumentos"} and the same thing on 2°radio.

When the code starts 1°radio already starts selected, however when it changes to 2°radio the console does not change. As I do?

Code:

/*Biblioteca*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;

class Layout {

    public static void main(String args[]) {

        EventQueue.invokeLater(() -> {

            /*cria o layout e a janela */
            JFrame frame = new JFrame("Prototipo10");
            JPanel panel = new JPanel(new GridBagLayout());


            /*Cria os Radio Butons*/
            GridBagConstraints gbc9 = new GridBagConstraints();
            gbc9.anchor = GridBagConstraints.WEST;
            gbc9.gridx = 1;
            gbc9.gridy = 7;
            JRadioButton radMasc = new JRadioButton("Radio1");
            radMasc.setForeground(Color.BLUE);

            GridBagConstraints gbc10 = new GridBagConstraints();
            gbc10.anchor = GridBagConstraints.WEST;
            gbc10.gridx = 1;
            gbc10.gridy = 8;
            JRadioButton radFem = new JRadioButton("Radio2");
            radFem.setForeground(Color.BLUE);

            ButtonGroup grubut = new ButtonGroup();
            grubut.add(radMasc);
            grubut.add(radFem);
            panel.add(radMasc, gbc9);
            panel.add(radFem, gbc10);


            /*Configurações da janela*/
            frame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setSize(250, 250);
            frame.getContentPane().add(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}
  • You did not add Listener?

  • I don’t know how to use the Istener

  • Got it + - how it works read some codes on the net but I was in doubt: Actionlistener has to be implemented in Buttongroup or direct on the radio? if yes and more what other it works?

  • No, you need to add to all the radiobuttons.

  • already understood how implements it, it works on all window menu type objects, Jtextfield, jButton among others?

1 answer

1


You need to create a ActionListener and apply it to each of the radiobuttons in your radiogroup:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;

public class JRadioButtonTest {

    public JRadioButtonTest() {

        /*cria o layout e a janela */
        JFrame frame = new JFrame("Prototipo10");
        JPanel panel = new JPanel(new GridBagLayout());


        /*Cria os Radio Butons*/
        GridBagConstraints gbc9 = new GridBagConstraints();
        gbc9.anchor = GridBagConstraints.WEST;
        gbc9.gridx = 1;
        gbc9.gridy = 7;
        JRadioButton radMasc = new JRadioButton("Radio1");
        radMasc.setForeground(Color.BLUE);

        GridBagConstraints gbc10 = new GridBagConstraints();
        gbc10.anchor = GridBagConstraints.WEST;
        gbc10.gridx = 1;
        gbc10.gridy = 8;
        JRadioButton radFem = new JRadioButton("Radio2");
        radFem.setForeground(Color.BLUE);

        ButtonGroup grubut = new ButtonGroup();
        grubut.add(radMasc);
        grubut.add(radFem);
        panel.add(radMasc, gbc9);
        panel.add(radFem, gbc10);

        radFem.addActionListener(new RadioButtonListener());
        radMasc.addActionListener(new RadioButtonListener());



        /*Configurações da janela*/
        frame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setSize(250, 250);
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(() -> new JRadioButtonTest());
    }

    class RadioButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JRadioButton radio = (JRadioButton) e.getSource();
            System.out.println(radio.getActionCommand());

        }

    }
}

Recommended reading:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.