Switch event listener

I would like to set an editable version of a text field based on a radio button selection? How to encode an action listener on a switch?

+9
source share
5 answers

This is the solution 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); } }); 
+7
source

My Java is a little rusty, but that should be what you are 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 installs it.

 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); 
+5
source

Try the following:

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

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"); } }); } 
+1
source

Another answer to this question. Change the small code from zalpha314 answer.

You can know which switch is selected by the text of this button, and you can also recognize it by the Action command. In the oracle demo code with a diagnosis of http://docs.oracle.com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java I learned how to use the action command.

First define two action commands

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

Then add the action command to the buttons

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

So, in actionPerformed you can get the action command.

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

The command action might be better if button.getText() is a very long string.

0
source

Source: https://habr.com/ru/post/919612/


All Articles