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

后端 未结 1 1249
天命终不由人
天命终不由人 2021-01-28 08:02

I have two radio buttons in a button group and in the same panel I have a text box and a button. I want to enable the text box and the button only when the second button is sele

1条回答
  •  -上瘾入骨i
    2021-01-28 08:36

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

    0 讨论(0)
提交回复
热议问题