开发者

Linking JComboBox & JTable

开发者 https://www.devze.com 2023-01-11 22:12 出处:网络
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 propertie

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(i.e 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)
       开发者_如何学Go {
            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:

  • http://www.camick.com/java/source/TableCellListener.java
  • http://tips4java.wordpress.com/2009/06/07/table-cell-listener/

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消