Action Listener on a radio button

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?

Answers


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);

        }
    });

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);

Try this:

JRadioButton myRadioButton = new JRadioButton("");
myRadioButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
      // Do something here...
    }
});

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.


Need Your Help

How does rails parse POST request parameters?

ruby-on-rails json ruby-on-rails-4 post parameters

I am working on an application where I send a post request to my rails server with parameters stored in JSON format. Let's say my application routes the request to the create function on my