升级JCombobox的项目

时间:2014-05-22 11:48:52

标签: java swing jtable jcombobox mouselistener

当我点击myJtabel中的一行时,我想升级myComboBox的列表项。我做了我必须做的事情才能取得好成绩。但是当一个未归档的myComboBox我有一个真实作为最后一项,并且每次我完成升级都会添加一个true作为项目

我班上没有的内容

myUpgradeCombo:

    class myUpgradeCombo {

public static <E> void upgrade_(JComboBox<E> combo, int index)
    {
        E item = combo.getItemAt(index);
        combo.removeItemAt(index);
        combo.insertItemAt(item, 0);
        combo.setSelectedIndex(0);
    }

    public static <E> boolean upgrade(JComboBox<E> combo, E item)
    {
        for (int index=0; index<combo.getItemCount(); index++)
        {
            if (combo.getItemAt(index).toString().equals(item.toString()))
            {
                upgrade_(combo,index);

                return true;
            }
        }
         return false ;
    }
}

按下myJtable鼠标时:

    int rec = myJtableF.getSelectedRow();
String idf = myJtableF.getValueAt(rec, 1).toString();
String format = myJtableF.getValueAt(rec, 2).toString();
String platef = myJtableF.getValueAt(rec, 3).toString(); 
this.myCombo.addItem(myUpgradeCombo.upgrade(myCombo, new FC(format,platef,idf)));

结果在myCombo中:

...... MySelectedItem
.......Firstitem
.......SecondItem
.......ThirdItem
.......fourthItem
.......true
.......true

谢谢,

1 个答案:

答案 0 :(得分:0)

因为myUpgradeCombo.upgrade(myCombo,new FC(format,platef,idf))的返回值传递给this.myCombo.addItem()。

所以从upgrade()返回的true作为项添加到你的组合框中,在这一行:

this.myCombo.addItem(myUpgradeCombo.upgrade(myCombo, new FC(format,platef,idf)));