例如,我有一个值为的JComboBox:
{"weapon","armor","weapon"}
如果当前选择的索引为0 (weapon)
并且我选择了索引2 (weapon)
,则它不会在我的ItemListener中触发ItemStateChanged。虽然如果当前选择的索引为0并且我选择索引1,则它会触发ItemStateChanged。
以下是我的代码:
class CBListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent e){
JComboBox temp = (JComboBox) e.getSource();
int wordIndex = temp.getSelectedIndex(); // index of selected string in the list
int row = sTable.getSelectedRow(); // row of the cell
int col = sTable.getSelectedColumn(); // column of the cell
sTable.setValueAt(listE.get(row)[wordIndex], row, 0);
//System.out.println(listE.get(row)[wordIndex]);
sTable.setValueAt(listI.get(row)[wordIndex], row, 1);
sTable.setValueAt(listD.get(row)[wordIndex], row, 3);
}
}
如何修改我的代码,以便在我的示例中获得类似索引2的索引?
答案 0 :(得分:0)
我仍然建议区分这两个项目,但是如果你真的希望它们是相同的,我认为你必须创建一个toString值为“weapon”的对象,但是对于它来说等于返回false。类似的东西:
public BuyableItem( String description, int index ) {
:
:
}
public String toString() {
return this.description;
}
public boolean equals( BuyableItem item ) {
boolean retVal = this.description.equals( item.description );
if( retVal ) {
retVal = this.index == item.index;
}
return retVal;
}
然后您的JComboBox值可能是
{ new BuyableItem( "weapon", 0 ), new BuyableItem( "armor", 1 ), new BuyableItem( "weapon", 2 ) };