簡體   English   中英

鏈接JComboBox和JTable

[英]Linking JComboBox & JTable

要求 :我在ComboBox中顯示了一個字符串列表。 每個字符串都可以具有一些屬性。 這些屬性顯示在PropertyTable中。 組合框的選定項目的屬性顯示在表中。 另外,我們使用PropertyTable編輯或設置comboBox中所選項目的屬性值。

問題:當我取消選擇comboBox Item時, 例如item1 ,PropertyTable中所有現有的屬性值都被設置為item1的新屬性值 再次,當我選擇這個item1時,我應該將上面的屬性值( 即,取消選擇item1之前的值 )返回到PropertyTable中?

當前的實現邏輯:

我為每個PropertyTableCell擁有TableCellListner ,每當更改單元格內容時,它都會使用單元格的新值並將其作為新屬性值分配給組合框的選定項。 每當選擇新項目時,都會使用所選項目的屬性值刷新表。

  //before is Table initialization code

Action action = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
        TableCellListener table = (TableCellListener)e.getSource();
        String selectedItem=(String)ComponentPropComboBox.getSelectedItem();
        if(table.getColumn()==1 && selectedItem!=null)
        {
            Property property=propertyMap.get(selectedItem);

            else if(table.getRow()==0)
            {
                property.setProperty("MIN_LENGTH", (String)table.getNewValue());
                propertyMap.put(selectedItem, property);
            }

            else if(table.getRow()==1)
            {
                property.setProperty("STARTS_WITH_STRING", (String)table.getNewValue());
                propertyMap.put(selectedItem, property);
            }
          }
    }
};

TableCellListener tcl = new TableCellListener(PropertiesTable, action);

如何通過克服上述挑戰來實現這一要求?

PS: TableCellListner是一個非Java通用庫。 您可以通過以下鏈接查看代碼及其解釋:

我相信問題很明顯! 請讓我知道問題是否還不清楚。在此先感謝您的幫助和捐贈知識!

在偵聽JComboBox選擇的代碼中。 在開始時,它會設置一個布爾值,指示要更改項目。 然后讓表刷新代碼忽略設置布爾值時發生的事件。 完成刷新后,請重新設置布爾值。

暫無
暫無

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

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