Toggle a component's 'enable' property according to a radio button in netbeans

不想你离开。 提交于 2019-12-02 09:35:05

You don't want to use an ActionListener because the event only fires when you click the button. Instead you can use an ItemListener so an event is generated when the item is selected or deselected (by clicking the other radio button). Something like:

radioButton2.addItemListener( new ItemListener()
{
    public void itemStateChanged(ItemEvent e)
    {
        JRadioButton button = (JRadioButton)e.getSource();
        component1.setEnabled( button.isSelected() );
        component2.setEnabled( button.isSelected() );
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!