简体   繁体   中英

Linking JComboBox & JTable

Requirement : I have a list of strings displayed in the ComboBox. Each of these Strings can have some properties. These properties are displayed in 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.

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. Again, when I select this item1 back, I should get above property values( ie values before item1 is Deselected ) back in to the 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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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