質問

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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top