繁体   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