Search code examples
javaswingjtablejcomboboxtablemodel

JComboBox sharing data with TableModel


I have a table with a column "Category" which is rendered with JComboBox. This table holds items Expense and is called "Expense table". JComboBox is created like this: JComboBox(new DefaultComboBoxModel()).

public class Expense {
  Tag category;
}

class Tag {
  String name;
}

I wanted to customize Categories. So I created a table "Categories" for adding, removing possible values to Categories. This table uses TagTableModel and operates on field data, which holds ArrayList value.

class TagTableModel 
  extends AbstractTableModel {
    ArrayList<Tag> data;
    ...
}

Once a user changes a value in "Categories" table: adds category, removes category, edits column "Name" on some row, I would like values in JComboBox to be updated as well.

What are possible ways to make JComboBox to rely on values from TagTableModel?


Solution

  • Thank you @kleopatra for the idea :)

    I used TableModelListener as suggested: created

    public class CategoryTableModelListener 
        implements TableModelListener
    {
        public void tableChanged(TableModelEvent e) 
        {
            /** here I get changed row and access object that was in this row and has changed */
        }
    }