英文:
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("提交");
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_5 = new JTable();
scrollPane.setViewportView(table_5);
model = new DefaultTableModel();
Object[] column = {"姓名", "员工编号", "电话号码", "排班"};
model.setColumnIdentifiers(column);
table_5.setModel(model);
这是下拉框(JComboBox)的数据:
cb1.setBounds(114, 113, 94, 22);
panel_1.add(cb1);
cb1.addItem("6:00-8:00 上午");
cb1.addItem("8:00-10:00 上午");
cb1.addItem("10:00-11:00 上午");
我的关注是,如果我选择下拉框中的第一个选项,我希望它完全被移除,这样就不能再选择它了。
英文:
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:
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.
答案1
得分: 1
在你澄清了你实际上是指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);
}
英文:
After you clarified that you actually meant JComboBox
instead of JCheckBox
this clearly makes more sense. I quickly put together a minimal reproducible example, which contains a JComboBox
including your timeframes and a submit button. 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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论