Action Listener on a radio button
Asked Answered
H

5

11

I would like to set editable option of a text box based on the selection of a radio button? How to code the action listener on the radio button?

Hennessey answered 3/7, 2012 at 18:15 Comment(2)
See the java Tutorials: docs.oracle.com/javase/tutorial/uiswing/components/button.html and docs.oracle.com/javase/tutorial/uiswing/events/… ?Shadchan
JCheckbox seems more apropos.Cussedness
C
8

This is the solution that I would use in this case.

    //The text field
    JTextField textField = new JTextField();

    //The buttons
    JRadioButton rdbtnAllowEdit = new JRadioButton();
    JRadioButton rdbtnDisallowEdit = new JRadioButton();

    //The Group, make sure only one button is selected at a time in the group
    ButtonGroup editableGroup = new ButtonGroup();
    editableGroup.add(rdbtnAllowEdit);
    editableGroup.add(rdbtnDisallowEdit);

    //add allow listener
    rdbtnAllowEdit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            textField.setEditable(true);

        }
    });

    //add disallow listener
    rdbtnDisallowEdit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            textField.setEditable(false);

        }
    });
Catapult answered 22/11, 2013 at 15:30 Comment(0)
B
6

My Java is a little rusty, but this should be what you're looking for.

Here is your listener:

private RadioListener implements ActionListener{

    private JTextField textField;

    public RadioListener(JTextField textField){
        this.textField = textField;
    }

    public void actionPerformed(ActionEvent e){
        JRadioButton button = (JRadioButton) e.getSource();

        // Set enabled based on button text (you can use whatever text you prefer)
        if (button.getText().equals("Enable")){
            textField.setEditable(true);
        }else{
            textField.setEditable(false);
        }
    }
}  

And here is the code that sets it up.

JRadioButton enableButton = new JRadioButton("Enable");
JRadioButton disableButton = new JRadioButton("Disable");

JTextField field = new JTextField();

RadioListener listener = new RadioListener(field);

enableButton.addActionListener(listener);
disableButton.addActionListener(listener);
Beryl answered 3/7, 2012 at 18:21 Comment(0)
P
3

Another answer for this question. Modify a little code from zalpha314 's answer.

You could know which radio button is selected by the text of this button, and you could also know it by Action Command. In the oracle's radio button demo code http://docs.oracle.com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java , I learnt how to use action command.

First, define two action command

final static String ON = "on"
final static String OFF = "off"

Then add action command to buttons

JRadioButton enableButton = new JRadioButton("Enable");
enableButton.setActionCommand(ON);
JRadioButton disableButton = new JRadioButton("Disable");
disableButton.setActionCommand(OFF);

So in actionPerformed, you could get the action command.

public void actionPerformed(ActionEvent e){
    String ac = e.getActionCommand();
    if (ac.equals(ON)){
        textField.setEditable(true);
    }else{
        textField.setEditable(false);
    }
}

Action command maybe better when the button.getText() is a very long string.

Peebles answered 15/12, 2015 at 16:19 Comment(0)
H
2

Try this:

JRadioButton myRadioButton = new JRadioButton("");
myRadioButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
      // Do something here...
    }
});
Helico answered 3/7, 2012 at 18:19 Comment(2)
It would be nice to show how we detect which radio button is active in the group of buttons.Philipson
Use ItemListener instead of ActionListener for ItemSelectable components like radio buttonReddick
O
0

Try this:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NewStudent {
    public static void main(String[] args){
        NewStudent st=new NewStudent();
    }

    public NewStudent(){
        JFrame frame=new JFrame("STUDENT REGISTRATION FORM");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,600);
        frame.setVisible(true);

        JPanel p1=new JPanel();
        p1.setLayout(null);
        p1.setBackground(Color.CYAN);

        frame.add(p1);
        ButtonGroup buttonGroup=new ButtonGroup();
        JRadioButton male=new JRadioButton("MALE");
        male.setBounds(100,170,100,20);
        buttonGroup.add(male);
        p1.add(male);
        JRadioButton female=new JRadioButton("FEMALE");
        female.setBounds(250,170,100,20);
        buttonGroup.add(female);
        p1.add(female);
        JLabel sex =new JLabel("SEX:");
        sex.setBounds(10,200,100,20);
        p1.add(sex);
        final JTextField gender= new JTextField();
        gender.setBounds(100,200,300,20);
        p1.add(gender);

        male.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ie){
                gender.setText("MALE");
            }
        });

        female.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ie){
            gender.setText("FEMALE");
        }
    });
}
Opalescent answered 17/4, 2019 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.