簡體   English   中英

JTable JComboBox默認在列表擴展時顯示第一項

[英]JTable JComboBox is defaulting to display 1st item when list is expanded

我一直在嘗試確定為什么我的JComboBox通過大量Google搜索顯示列表中的第一項,但是我一直在努力尋找相關的幫助。 可能是因為我不知道正確的術語(因此這個問題的標題太具體了),因此找不到可解釋我問題的信息。 我簽出了JComboBox API,以及它使用的一些偵聽器和模型,但是它們似乎不太可能成為候選對象。

有問題的JComboBox位於JTable內部,因此我不知道這是否會更改其默認行為。 我使用的代碼如下:

//row and col are final due to usage inside anonymous inner class
public TableCellEditor getCellEditor(final int row, final int col)
{
    String[] listItems = new String[arrayList.getSize()];
    int i = -1;

    for(String s : arrayList)
    {
        i++;
        listItems[i] = s;
    }

    JComboBox<String> box = new JComboBox<>(listItems);
    box.addItemListener(new ItemListener()
    {
        public void itemStateChanged(ItemEvent e)
        {
            if(e.getStateChange() == ItemEvent.SELECTED)
            {
                if(e.getItem().equals("Add/Edit Projectile"))
                {
                    //Where Editor is a JFrame that will be opened 
                    new Editor();
                }
            }
        }
    });

    DefaultCellEditor list = new DefaultCellEditor(box);
}

請注意,我程序中的Arraylist不包含字符串,而是一組更復雜的自定義對象,我相信這些對象會分散主要問題的注意力。

我沒有在JTable中包含JComboBox的渲染器,因為我對它的顯示方式非常滿意,並發現我的問題更多是我在模型/實現錯誤中忽略了。

我還提供了一些屏幕截圖,以更好地描述我的問題。 第一個圖像是未選擇JComboBox時,僅顯示當前選擇的項目。 未選中,默認狀態

第二張圖片是當我單擊JComboBox彈出列表時。 如圖所示,無論它是什么,它都會立即顯示該第一項。 已選擇,列表已展開

如果有人對在哪里查找/解決方案有任何建議,我將不勝感激。


編輯

我的特定表有兩列,其中左列是變量名,右列是與變量關聯的值。 表的作用是顯示所選對象的屬性,其中不同對象的不同變量的每個值可能都不相同。

在這種情況下,該單元會顯示一個JComboBox,其中包含我們正在制作的游戲中所有可用的投射物。 每個敵人都有默認的不同類型的彈丸。 因此,當我在游戲區域中單擊其他敵人時,該表將顯示其所有當前屬性(如果尚未更改,則為默認屬性)。

敵人的確有射彈的吸氣劑,因此我可以確定當前選擇的敵人是什么,使其成為射彈,並執行toString()以查找其在列表中的表示方式,然后執行setValueAt()。

唯一的問題是,當列表擴展時,它始終會選擇列表中的第一項。

除非為每行動態生成JComboBox的值,否則您應該能夠提前准備CellEditor ,例如...

JComboBox cb = new JComboBox(new String[]{"1", "2", "3", "4"});
DefaultCellEditor editor = new DefaultCellEditor(cb);

JTable table = new JTable(new DefaultTableModel(5, 1));
table.getColumnModel().getColumn(0).setCellEditor(editor);

當編輯過程開始時,這會將編輯器的選定值設置為單元格的值

在此處輸入圖片說明在此處輸入圖片說明

更新

在每行動態生成組合框值的情況下,您可以執行更多類似的操作...

JComboBox cb = new JComboBox();
DefaultCellEditor editor = new DefaultCellEditor(cb) {

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        JComboBox editor = (JComboBox) getComponent();
        String[] listItems = new String[arrayList.getSize()];
        int i = -1;
        for (String s : arrayList) {
            i++;
            listItems[i] = s;
        }

        DefaultComboBoxModel model = new DefaultComboBoxModel(listItems);
        editor.setModel(model);
        editor.setSelectedItem(value);
        return editor;
    }
};

JTable table = new JTable(new DefaultTableModel(5, 1));
table.getColumnModel().getColumn(0).setCellEditor(editor);

注意使用editor.setSelectedItem(value); ,這會將所選值設置為單元格的當前值...

您還可以重新使用模型,每次都將其清除,並用新值重新填充它。 如果行數很多,您可能會發現這種方法更有效,因為每次編輯單元格時都不需要不斷創建新模型

這是老歌...

您的問題很可能是您沒有在組合中使用的類中實現“等於”。

組合需要在准備當前項目時選擇它,方法是遍歷模型的各個元素並選擇與單元格中的值相等的第一個項目。 如果沒有遇到任何問題,則它將組合保持不變(上一個單元格編輯中的第一個元素或最后一個使用的元素)

這是默認應使用先前選擇的元素的方式:

//...
Object selectedItem = box.getSelectedItem();
//Add some elements to the jComboBox
box.setSelectedItem(selectedItem);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM