Java - 启用组合框

时间:2017-09-07 13:09:21

标签: java swing combobox radio-button jradiobutton

我的代码如下所示:

public class ReservationsWindow extends JFrame {

        private JLabel lblDriver;
        private JComboBox cbDriver;

        private JComboBox cbDispathcer;
        private JLabel lblDispatcher;

        private JLabel lblVehicle;
        private JComboBox cbVehicle;

        private JRadioButton rbMobileApp;
        private JRadioButton rbTelephoneCall;
        private ButtonGroup bgOrder;

        private JLabel lblNote;
        private JTextField txtNote;

        private JButton btnAplly;

        private void initActions() {
            rbMobileApp.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    rbMobileApp.addItemListener(evt -> {
                        cbDriver.setEnabled(evt.getStateChange() == ItemEvent.SELECTED);
                        txtNote.setEnabled(evt.getStateChange() == ItemEvent.SELECTED);
                    });
                }
            });
            rbTelephoneCall.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    rbTelephoneCall.addItemListener(evt -> {
                        cbDispatcher.setEnabled(evt.getStateChange() == ItemEvent.SELECTED);
                    });
                }
            });

我想根据检查哪个radiobutton启用组合框。但是,当窗口第一次打开所有组合框都被启用时,我必须点击其中一个(并且它仍然启用了所有内容),只有当我再次点击其他单选按钮时,一切正常。我想在第一次点击时一切正常。如何解决? enter image description here enter image description here

enter image description here

0 个答案:

没有答案