Identify JRadioButton selection

0

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

When the code starts the 1st radio already starts selected, but when it changes to the 2nd radio the console does not change. How do I do it?

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);
        });
    }
}
    
asked by anonymous 22.10.2017 / 16:47

1 answer

1

You need to create a ActionListener and apply it to each of the radiobuttons that is 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:

22.10.2017 / 17:00