[英]How to automatically remove an item in jcombobox when I already select it?
This is the submit button code:这是提交按钮代码:
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);
}
});
This is the table code:这是表代码:
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);
This is the data of jcombobox:这是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");
My concern is if i select the first option in the jcheckbox I want to removed it completely so It will not choose again.我担心的是,如果我选择 jcheckbox 中的第一个选项,我想将其完全删除,以免再次选择。
After you clarified that you actually meant JComboBox
instead of JCheckBox
this clearly makes more sense.在您澄清您实际上是指JComboBox
而不是JCheckBox
这显然更有意义。 I quickly put together a minimal reproducible example, which contains a JComboBox
including your timeframes and a submit button.我很快整理了一个最小的可重现示例,其中包含一个JComboBox
其中包括您的时间范围和一个提交按钮。 On the click of the submit button the currently selected timeframe will be removed from the combobox.单击提交按钮时,当前选定的时间范围将从组合框中删除。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.