我使用JComboBox
呈现了列,我有标签显示JComboBox
内每种类型的所选项目数量。
问题是我想为每一行触发不同的ItemListener
。
现在我有一个列,其中所有单元格都呈现JComboBox
,但所有单元格都触发ItemListener
为什么?
这让我很紧张。
这是我的代码:
listaPessoasModel = new DefaultTableModel(dadosPessoasTableColumn, 0);
listaPessoasTable = new JTable(listaPessoasModel);
listaPessoasTable.addKeyListener(newLineTableListener);
listaPessoasTable.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(listaPessoasTable);
scrollPane.setBounds(205, 344, 385, 169);
TableColumn classeColumn = listaPessoasTable.getColumnModel().getColumn(2);
classeColumn.setCellRenderer(new ClasseTableCellRender());
classeComboBox = new JComboBox<Classe>(new Vector<Classe>(Classe.allInstances()));
classeComboBox.setSelectedIndex(-1);
classeColumn.setCellEditor(new DefaultCellEditor(classeComboBox));
classeComboBox.setRenderer(classeComboBoxRenderer);
classeComboBox.addItemListener(new classeComboBoxItemListener());
这里是我所有的听众和模特:
private final class classeComboBoxItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
Classe object = (Classe) e.getItem();
System.out.println("ItemListener , objecto -> " + object);
if(object != null && !newItemClasseComboControl){
if(e.getStateChange() == ItemEvent.SELECTED)
updateLabelLugares(false, object);
if(e.getStateChange() == ItemEvent.DESELECTED)
updateLabelLugares(true, object);
}
}
}
static class ClasseTableCellRender extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
public ClasseTableCellRender() { super(); }
public void setValue(Object value) {
if(value instanceof Classe)
setText((value == null) ? "" : ((Classe) value).tipo().toString());
else
setText((value == null) ? "" : value.toString());
}
}
private ListCellRenderer<Object> classeComboBoxRenderer = new DefaultListCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList<?> list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Classe classe = (Classe) value;
if(value != null)
value = classe.tipo().toString();
return super.getListCellRendererComponent(list, value,
index, isSelected, cellHasFocus);
}
};
以上是截图,显示了我要找的内容 这些数字只是代表这个想法,因为这仍然没有用。我不能让项目监听者按照我想要的方式为每一行单独工作 因为当我单击打开ComboBox时,它会取消选择之前选择的项目 我希望项目监听器仅在jcombobox内的项目选择时触发,并且该列的每个单元格都是单独的,因此我可以正确更新标签。
答案 0 :(得分:0)
这只是因为可能是它没有为Combobox获得适当的价值使用这个TableCellEditor可能会有所帮助。
class CheckBoxCellRenderer implements TableCellRenderer {
JComboBox combo;
JSONObject response;
public CheckBoxCellRenderer(JComboBox comboBox) {
this.combo = new JComboBox();
combo.setForeground(new Color(77, 75, 71));
for (int i = 0; i < comboBox.getItemCount(); i++) {
combo.addItem(comboBox.getItemAt(i));
}
combo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent ie) {
System.out.println("Item state changed --");
}
});
}
public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (jtable.getSelectedRow() == row) {
combo.setSelectedItem(value);
combo.setActionCommand("" + row);
return combo;
}
JComboBox box = new JComboBox();
box.addItem("Pending");
return box;
}
}