The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Table Model  [1 examples]

e963. Sharing a Table Model Between JTable Components

When you share a table model between two table components, any changes made to values in the model will appear in both table components. However, any changes to the visible columns in one table component will not affect the columns in the other table component.
    DefaultTableModel model = new DefaultTableModel();
    JTable table1 = new JTable(model);
    JTable table2 = new JTable(model);
    
    // Add data here
    
    // Place the two tables in a split pane
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.add(new JScrollPane(table1));
    splitPane.add(new JScrollPane(table2));
    
    // Remove the first visible column from table1;
    // the column will not be removed from table2
    table1.getColumnModel().removeColumn(table1.getColumnModel().getColumn(0));

 Related Examples

See also: Cells    Column Heads    Columns    Editing    Events    Layout    Rows    Scrolling    Selection    Sorting    Tool Tips   


© 2002 Addison-Wesley.