I am currently working with two JTable. Each of them has its own custom TableModel.
In the first JTable i have items that can be select (checkbox). The second one starts empty. When i check a line in the first table, the backgroundColor of the line changes (thanks to a preparedRenderer method) and i would like the content of the checked line to be added in the second JTable.
I suppose i should use a TableModelListener but i don't see how i tell the second Table (and its model) to listen to the first one.
Does anyone have some thought about that ?
Here is a sample of what the code i got look like :
public class MyClass {
private Model1 model1;
private Model2 model2;
private JTable table1;
private JTable table2;
public void myMethod()
{
table1 = new JTable();
model1 = new Model1();
table1.setModel(model1);
table1.getModel().addTableModelListener(new TableModelListener()
{
@Override
public void tableChanged(TableModelEvent e)
{
//Here some code to enable a button when at least one row is checked
//that works fine
}
});
table2 = new JTable();
model2 = new Model2();
table2.setModel(model2);
}
}
Thanks for your help :)
You can update model2 in the listener of model1 and refresh its table2:
public class MyClass {
private Model1 model1;
private Model2 model2;
private JTable table1;
private JTable table2;
public void myMethod()
{
table1 = new JTable();
model1 = new Model1();
table1.setModel(model1);
table2 = new JTable();
model2 = new Model2();
table2.setModel(model2);
table1.getModel().addTableModelListener(new TableModelListener()
{
@Override
public void tableChanged(TableModelEvent e)
{
//Here some code to enable a button when at least one row is checked
//that works fine
Object aValue = "something"; //fill
int row = 1; //fill
int column = 1; //fill
model2.setValueAt(aValue, row, column);
model2.fireTableDataChanged();
}
});
}
}
working with two JTable. Each of them has its own custom TableModel.
In the first JTable i have items that can be select (checkbox). The second one starts empty. When i check a line in the first table, the backgroundColor of the line changes (thanks to a preparedRenderer method)
don't to use TableModelListener
, override setValueAt
(Each of them has its own custom TableModel.
) for first XxxTableModel
e.g. tableModelFirts.setValueAt(whatever_linked_in_second_model)
TableModelListener
could not be used as notifier to change value in model, inside or outside
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With