Akk*_*kki 5 java swing jcombobox
1.当用户从JComboBox中选择项目时,我创建了一个JComboBox和Jtable,它们被添加到JTable中.
2.我不想让用户选择他之前在JComboBox中选择的项目.
3.因此必须禁用所选的选项(不可选择).我该怎么办?4.下面的代码在JTable中添加后从JComboBox中删除了该项,但我有兴趣禁用它
String getchoice=(String)selectedgames_combobox.getSelectedItem();
DefaultTableModel gamesmodel = new DefaultTableModel();
//adding selected choices from JComboBox in JTable
gamesmodel.addColumn("Selected Games");
gamesmodel.insertRow(gamesmodel.getRowCount(),new Object[]{ getchoice }) ;
//refreshing table
games_table.setModel(gamesmodel);
//removing the selected item from JComboBox
selectedgames_combobox.removeItem(getchoice);
Run Code Online (Sandbox Code Playgroud)
假设您有一个列表,其中包含一些应禁用的元素,您需要更改已禁用元素的外观,并且需要阻止用户选择这些项目.为了能够阻止用户选择禁用的,你需要覆盖这样的setSelectedIndex方法JComboBox:
public void setSelectedIndex(int index) {
if (!disabled_items.contains(index)) {
super.setSelectedIndex(index);
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以BasicComboBoxRenderer像这样更改项目的颜色:
if (disabled_items.contains(index)) {
setBackground(list.getBackground());
setForeground(UIManager.getColor("Label.disabledForeground"));
}
Run Code Online (Sandbox Code Playgroud)