Java ListSelectionModel & Multiple Related Lists/Tables

Status
Not open for further replies.
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):
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)
 
Faramir":3rhkcog3 said:
Did you try stepping through with a debugger? Where is it getting stuck?

Edit:
It looks like this line is repeatedly triggering the valueChanged listener:
Code:
  targetTable.getSelectionModel().addSelectionInterval(targetRow, targetRow);
I don't work with swing, so I'm not sure if there is anything wrong with it.

It's not so much stuck as mutually recursive without end ...

User selects a row in table A.

Table A's selection listener fires and selects related row(s) in Table B.

Table B's selection listener fires and selects related row(s) in Table A.

Table A's selection listener fires and selects related row(s) in Table B.

...

Repeat until stack overflows.

I tried mucking about with the event's valueIsAdjusting() check, but it doesn't help. I also tried setting the SelectionModel's isAdjusting to true before the adjustment then false afterward, but the recursive events keep firing.
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
zelmak":1k6eopoc said:
Faramir":1k6eopoc said:
Code:
      if (compareValue != null && values.contains(compareValue)) {
        targetTable.getSelectionModel().setValueIsAdjusting(true);
        targetTable.getSelectionModel().addSelectionInterval(targetRow, targetRow);
      }

With this line it seems to work for me.

try uncommenting the third listener :)

I did. But now looking at it again, I made one other change:
Code:
    table.setSelectionModel().addListSelectionListener(this);
gave me an eclipse error, so I changed it to:
Code:
    table.getSelectionModel().addListSelectionListener(this);

So the code that WFM for me has:
A package declaration
uncommented third listener
the change from set to get
and the added line re: setValueIsAdjusting
 
Faramir":1gf0tzdm said:
zelmak":1gf0tzdm said:
Faramir":1gf0tzdm said:
Code:
      if (compareValue != null && values.contains(compareValue)) {
        targetTable.getSelectionModel().setValueIsAdjusting(true);
        targetTable.getSelectionModel().addSelectionInterval(targetRow, targetRow);
      }

With this line it seems to work for me.

try uncommenting the third listener :)

I did. But now looking at it again, I made one other change:
Code:
    table.setSelectionModel().addListSelectionListener(this);
gave me an eclipse error, so I changed it to:
Code:
    table.getSelectionModel().addListSelectionListener(this);

So the code that WFM for me has:
A package declaration
uncommented third listener
the change from set to get
and the added line re: setValueIsAdjusting

Thanks. I knew I missed another typo. I'll have another look when I get home.
 
Status
Not open for further replies.