0

I have several JTables with several columns each. Each table is displayed on a different tab in a JTabbed Pane. These columns have the same header name among the tables, but different values in the columns. I am trying to allow the comparison of a header among all of the tables by right clicking a header, and hitting compare. This opens a new JDialog while passing a List of columns with the matching header that was chosen:

public void actionPerformed(ActionEvent arg0) {
                List<TableColumn> columns = new ArrayList<TableColumn>();

                for (int i = 0; i < tables.size(); i++) {

                    String tableName = tabPane.getTitleAt(i);
                    JTable tempTable = (JTable) tables.get(tableName);

                    // Get column at the channel name used
                    TableColumn col = tempTable.getColumn("chosen header");

                    // Add the column to the list of channel columns
                    columns.add(col);
                }

                new comparisonDialog(UI.getFrame(), "chosen header",
                        columns);
            }

This appears to work correctly, storing all of the common columns for the tables into a list that is passed to a new JDialog. It also seems to work when I display a table with these columns in the new JDialog:

JTable table = new JTable();

for (TableColumn col : passedColumnList) {
            col.setHeaderValue(col.getHeaderValue());
            table.addColumn(col);
        }

The correct columns are displayed, however:

  • All of the data in the column is missing
  • The columns seem to be linked to the tables they came from, as resizing one resizes the column in the table it came from!

Am I missing something simple here?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ricgeorge
  • 188
  • 3
  • 5
  • 12
  • there are two / three ways, but I missing additional important infos (no battery included), for better help sooner post an [SSCCE](http://sscce.org/) – mKorbel Oct 11 '12 at 08:31

1 Answers1

3

Firstly, data in the tables is controlled by the table model, not by the column model.

Secondly, a TableColumn has a model of its own (wrapped in a TableColumnModel), changes to the object will be reflected by all those models registered to monitor it (and therefore the views that rely on them).

You will need to create a new table model containing the rows of data for each column, mapped to the appropriate column.

Instead of "extracting" the column reference from the underlying tables, you should allow your table model to define the column information.

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • This makes sense, thank you. I imagine I will have to use a custom table model to get the values in the correct columns? I cannot figure out how to do it with the default table model, as all of the values end up in the first column. – ricgeorge Oct 11 '12 at 09:47
  • 1
    You could get it to work with a default model, I personally starting a `AbstractTableModel`. Basically, you need to figure out way to map each column to the rows of data they're suppose to represent – MadProgrammer Oct 11 '12 at 09:50
  • Thanks a lot, I've finally got it working correctly using a default table model :) – ricgeorge Oct 11 '12 at 10:30