简体   繁体   English

链接JComboBox和JTable

[英]Linking JComboBox & JTable

Requirement : I have a list of strings displayed in the ComboBox. 要求 :我在ComboBox中显示了一个字符串列表。 Each of these Strings can have some properties. 每个字符串都可以具有一些属性。 These properties are displayed in PropertyTable. 这些属性显示在PropertyTable中。 ComboBox's selected Item's properties are displayed in the table. 组合框的选定项目的属性显示在表中。 In addition, we use PropertyTable for editing or setting property values to the selected item in the comboBox. 另外,我们使用PropertyTable编辑或设置comboBox中所选项目的属性值。

Problem: The moment I de-select the comboBox Item, say item1 , all the existing property values in the PropertyTable are set as new property values to item1. 问题:当我取消选择comboBox Item时, 例如item1 ,PropertyTable中所有现有的属性值都被设置为item1的新属性值 Again, when I select this item1 back, I should get above property values( ie values before item1 is Deselected ) back in to the PropertyTable? 再次,当我选择这个item1时,我应该将上面的属性值( 即,取消选择item1之前的值 )返回到PropertyTable中?

Current Implementation Logic: 当前的实现逻辑:

I am having TableCellListner for each PropertyTableCell, whenever cell content is changed, it takes the cell's new value and assigns this as new property value to the combo box's selected item. 我为每个PropertyTableCell拥有TableCellListner ,每当更改单元格内容时,它都会使用单元格的新值并将其作为新属性值分配给组合框的选定项。 whenever new item is selected, table is refreshed with the selected Item's property values. 每当选择新项目时,都会使用所选项目的属性值刷新表。

  //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);

How do i implement this requirement by overcoming the above challenge? 如何通过克服上述挑战来实现这一要求?

PS: TableCellListner is a Not a java generic library. PS: TableCellListner是一个非Java通用库。 You can view code and its explanation at the following links: 您可以通过以下链接查看代码及其解释:

I believe the question is obvious! 我相信问题很明显! Pls do let me know if question is not clear.Thanks in advance for your help & donating the knowledge! 请让我知道问题是否还不清楚。在此先感谢您的帮助和捐赠知识!

In the code that listens for JComboBox selections. 在侦听JComboBox选择的代码中。 At its start have it set a boolean that the item is being changed. 在开始时,它会设置一个布尔值,指示要更改项目。 Then have your table refresh code ignore events that come while the boolean is set. 然后让表刷新代码忽略设置布尔值时发生的事件。 After you are finished refreshing then set the boolean back. 完成刷新后,请重新设置布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM