Hello,
I'm trying to make a multi-list/multi-table GUI where the list/tables with related information share the selection. If I click on a row in table 1, which has matching information in table 2, all the stuff in table 2 is subsequently selected. When the stuff in table 2 is then selected, it selects related information in table 3.
I've got this 'uni-directional' selection mode working fairly easily. Where it falls apart (with stack overflow exceptions) for obvious reasons is when I try to set it up to go back the other way (user selects stuff in table 3, selects related stuff in table 2, which subsequently selects related stuff in table 1.)
Here's a sample of what I've got as an example (I had to transcribe this from my dev environment so it may contain some typos):
There's got to be a workable solution to this ... am I completely missing some obvious way to do this?
(edit: fixed a couple obvious typos in the code)
I'm trying to make a multi-list/multi-table GUI where the list/tables with related information share the selection. If I click on a row in table 1, which has matching information in table 2, all the stuff in table 2 is subsequently selected. When the stuff in table 2 is then selected, it selects related information in table 3.
I've got this 'uni-directional' selection mode working fairly easily. Where it falls apart (with stack overflow exceptions) for obvious reasons is when I try to set it up to go back the other way (user selects stuff in table 3, selects related stuff in table 2, which subsequently selects related stuff in table 1.)
Here's a sample of what I've got as an example (I had to transcribe this from my dev environment so it may contain some typos):
The values are contrived, but the functionality is not.
Code:
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class AllInOne {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AllInOne().createAndDisplayGUI();
}
});
}
private void createAndDisplayGUI() {
Object[][] things = {
{ "Orange", "Fruit" },
{ "Car", "Ground Transport" },
{ "Tomato", "Fruit" },
{ "Jet", "Air Transport" },
{ "Rhubarb", "Vegetable" }
};
String[] thingColumns = { "Thing", "Sub-Type" };
Object[][] subTypes = {
{ "Ground Transport", "Transport" },
{ "Fruit", "Food" },
{ "Vegetable", "Food" },
{ "Air Transport", "Transport" }
};
String[] subTypeColumns = { "Sub-Type", "Type" };
Object[][] types = {
{ "Transport", "Inedible" },
{ "Food", "Edible" }
};
String[] typeColumns = { "Type", "More Info" };
JFrame f = new JFrame();
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
f.setContentPane(p);
JTable thingTable = new JTable(things, thingColumns);
JScrollPane scroller = new JScrollPane(thingTable);
f.add(scroller);
JTable subTypeTable = new JTable(subTypes, subTypeColumns);
scroller = new JScrollPane(subTypeTable);
f.add(scroller);
JTable typeTable = new JTable(types, typeColumns);
scroller = new JScrollPane(typeTable);
f.add(scroller);
ListSelectionListener listener = null;
listener = new TestSelectionListener(thingTable, "Sub-Type", subTypeTable, "Sub-Type" );
listener = new TestSelectionListener(subTypeTable, "Type", typeTable, "Type" );
// uncomment following to create feedback loop and stack overflow on click of center table
// listener = new TestSelectionListener(subTypeTable, "Sub-Type", thingTable, "Sub-Type" );
f.pack();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
}
class TestSelectionListener implements ListSelectionListener {
private JTable sourceTable;
private JTable targetTable;
private String sourceColumnName;
private String targetColumnName;
public TestSelectionListener(JTable table, String columnName, JTable otherTable, String otherColumn) {
sourceTable = table;
targetTable = otherTable;
sourceColumnName = columnName;
targetColumnName = otherColumn;
table.getSelectionModel().addListSelectionListener(this);
}
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}
// gather all unique values from selected items in source table's column
int[] selectedRows = sourceTable.getSelectedRows();
Set<Object> values = new TreeSet<Object>();
for (int selectedRow : selectedRows) {
Object value = sourceTable.getValueAt(selectedRow, getViewColumnFromColumnName(sourceTable, sourceColumnName));
if (value != null) {
values.add(value);
}
}
// now find all matching values in target table's column
targetTable.getSelectionModel().clearSelection();
// loop through all target table's rows; if match, add to selection
int targetTableRowCount = targetTable.getRowCount();
for (int targetRow = 0; targetRow < targetTableRowCount; targetRow++) {
Object compareValue = targetTable.getValueAt(targetRow, getViewColumnFromColumnName(targetTable, targetColumnName));
if (compareValue != null && values.contains(compareValue)) {
targetTable.getSelectionModel().addSelectionInterval(targetRow, targetRow);
}
}
}
private static int getModelColumnFromColumnName(JTable tbl, String name) {
TableModel mdl = tbl.getModel();
int column = 0;
while ( column < mdl.getColumnCount() ) {
if ( mdl.getColumnName(column).equals(name)) {
break;
}
column++;
}
return column;
}
private static int getViewColumnFromColumnName(JTable tbl, String name) {
return tbl.convertColumnIndexToView(getModelColumnFromColumnName(tbl, name));
}
}
There's got to be a workable solution to this ... am I completely missing some obvious way to do this?
(edit: fixed a couple obvious typos in the code)