当我已经 select 时,如何自动删除 jcombobox 中的项目?

How to automatically remove an item in jcombobox when I already select it?

这是提交按钮代码:

JComboBox cb1 = new JComboBox();
    Object[] row = new Object [4];
    JButton btnSubmit = new JButton("SUBMIT");
    btnSubmit.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
    btnSubmit.setBounds(35, 153, 84, 23);
    panel_1.add(btnSubmit);
    btnSubmit.addActionListener(new ActionListener() {
        
        public void actionPerformed(ActionEvent e) {
            row[0] = txtfieldname.getText();
            row[1] = txtfieldemail.getText();
            row[2] = txtfieldphone.getText();
            row[3] = cb1.getSelectedItem();
            model.addRow(row);
        }
            
        
    });

这是 table 代码:

table_5 = new JTable();
    scrollPane.setViewportView(table_5);
    model = new DefaultTableModel();
    Object[] column = {"Name","Employeed ID", "Phone No.", "Schedule"};
    model.setColumnIdentifiers(column);
    table_5.setModel(model);

这是jcombobox的数据:

cb1.setBounds(114, 113, 94, 22);
    panel_1.add(cb1);
    cb1.addItem("6:00-8:00 AM");
    cb1.addItem("8:00-10:00 AM");
    cb1.addItem("10:00-11:00 AM");

我担心的是如果我 select jcheckbox 中的第一个选项我想完全删除它所以它不会再次选择。

在你澄清你实际上是指 JComboBox 而不是 JCheckBox 之后,这显然更有意义。我很快整理了一个最小的可复制示例,其中包含一个 JComboBox,包括您的时间范围和一个提交按钮。单击提交按钮后,当前选定的时间范围将从组合框中删除。

public static void main(String args[]) {
    SwingUtilities.invokeLater(() -> {
        buildGui();
    });
}

private static void buildGui() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel("Select your timeframe: ");
    frame.add(label, BorderLayout.WEST);
    JComboBox<String> comboBox = new JComboBox<String>();
    comboBox.addItem("6:00-8:00 AM");
    comboBox.addItem("8:00-10:00 AM");
    comboBox.addItem("10:00-11:00 AM");
    frame.add(comboBox);
    JButton submitButton = new JButton("Submit");
    submitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // add row to your model
            // then remove the selected timestamp from your box
            comboBox.removeItemAt(comboBox.getSelectedIndex());
        }
    });
    frame.add(submitButton, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}