I have to make three radio buttons. On selection of the third radio button I have to show some text boxes. I also have to immediately remove the text boxes when the third radio button is unselected and any one of the other two is selected. I am looking for a callback when the JRadio button is unselected. I have supplied an ActionListener but it gets called only on selection and not on unselection. How do I achieve this toggle functionality with java JRadio button.

radioCustomButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Callback called");
            if(radioCustomButton.isSelected()){
                System.out.println("Checked");
            }
        }
    });

This is printing "Callback called" only on check but not on uncheck. I have added this radioCustomButton object in ButtonGroup as well.

I am left with the only option of supplying action listener to other two radio buttons and then write code to remove the text boxes.

有帮助吗?

解决方案

Don't use an ActionListener to check when the button is selected.

Instead use an ItemListener and you will be notified when the button is selected and unselected.

See How to Write an Item Listener for an example.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top