JDK-4816146 : 1.4.0 REGRESSION: JTable - Sharing TableColumnModels disables resizing of column
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-02-10
  • Updated: 2019-12-17
  • Resolved: 2019-12-17
Related Reports
Relates :  
Description
Name: jk109818			Date: 02/10/2003


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)


FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
If you use the same instance of a tableColumnModel in
different tables it's impossible to resize the columns via
mouse in all of the tables except the last one the model is
set to.


REGRESSION.  Last worked in version 1.3.1

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. compile and run the sample code as is
2. on startup only one table is showing, verify that the
columns are resizable by mouse
3. press the button to insert a second table (which uses the
same columnModel)
4. verify that the columns of the second table are resizable
by mouse (and the columns of the first table are resized
synchronously)
5. verify that nothing happens if you try to resize the
columns of the first table by mouse: neither the columns in
the first nor in the second table are resized.
6. press the button to add another table
7. verify that only the columns of that last table are
resizable by mouse


EXPECTED VERSUS ACTUAL BEHAVIOR :
in 5) the columns of the first table should be resized as
well as those in the second table.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

import java.awt.event.*;
import java.awt.*;

//import de.kleopatra.support.debugon.*;

/** Problem:
 *  if columnModel is shared between tables resizing of columns via mousedrag
 *  in header is not working in any of them except the last.
 *
 *  $RCSfile: TestSharingColumnModel.java,v $ $Revision: 1.1.2.2 $ $Date:
2003/01/07 13:08:45 $
 *
 */
public class TestSharingColumnModel {

  protected JFrame frame;

  protected final int ROWS = 15;
  protected final int COLUMNS = 3;
  protected TableModel model;
  protected TableColumnModel columnModel;
  protected JComponent mainPanel;

  public TestSharingColumnModel() {
    initModels();
    frame = new JFrame("TestSharingColumnModel");
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.getContentPane().add(buildMainPanel());
    frame.getContentPane().add(buildButtonPanel(), BorderLayout.SOUTH);
    frame.pack();
    frame.show();
  }

  //---------------------------init ui
  protected void initModels() {
    model = createTableModel(ROWS, COLUMNS);
    columnModel = createColumnModel(model);
  }

  protected JComponent buildMainPanel() {
    mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
    mainPanel.add(getScroll());
    return mainPanel;
  }

  protected JComponent getScroll() {
    JTable table = createTable(ROWS, COLUMNS);
    table.setAutoCreateColumnsFromModel(false);
    table.setModel(model);
    table.setColumnModel(columnModel);
    return new JScrollPane(table);
  }

  protected JComponent buildButtonPanel() {
    JPanel panel = new JPanel();
    JButton button = new JButton(createDefaultAction());
    frame.getRootPane().setDefaultButton(button);
    panel.add(button);
    return panel;
  }

  //---------------------------factory methods
  protected Action createDefaultAction() {
    Action action = new AbstractAction("add table") {

      public void actionPerformed(ActionEvent event) {
        mainPanel.add(getScroll());
        mainPanel.revalidate();
      }
    };
    String name = (String) action.getValue(action.NAME);
    action.putValue(action.MNEMONIC_KEY,
        new Integer(Character.toUpperCase(name.charAt(0))));
    return action;
  }

  protected JTable createTable(int r, int c) {

    JTable table = new JTable();
/*
    // including the following will fix the bug
    {

      public void columnMarginChanged(ChangeEvent e) {
        if (isEditing()) {
          removeEditor();
        }
        TableColumn resizingColumn = null;
        if (tableHeader != null) {
          resizingColumn = tableHeader.getResizingColumn();
        }
        if (resizingColumn != null) {
          if (autoResizeMode == AUTO_RESIZE_OFF) {
            resizingColumn.setPreferredWidth(resizingColumn.getWidth());
          } else {    // this else block is missing in jdk1.4 as compared to 1.3
            doLayout();
            repaint();
            return;
          }
        }
        resizeAndRepaint();
      }

      private int viewIndexForColumn(TableColumn aColumn) {
        TableColumnModel cm = getColumnModel();
        for (int column = 0; column < cm.getColumnCount(); column++) {
          if (cm.getColumn(column) == aColumn) {
            return column;
          }
        }
        return -1;
      }
    };
*/
    // out-comment to verify that resizing mode near the base of the problem
    // table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
    return table;
  }

  protected TableModel createTableModel(int r, int c) {
    return new DefaultTableModel(r, c);
  }

  protected TableColumnModel createColumnModel(TableModel model) {
    TableColumnModel columnModel = new DefaultTableColumnModel();
    for (int i = 0; i < model.getColumnCount(); i++) {
      TableColumn column = new TableColumn(i);
      column.setHeaderValue(model.getColumnName(i));
      columnModel.addColumn(column);
    }
    return columnModel;
  }

  //---------------------------Main
  public static void main(String[] args) {
    new TestSharingColumnModel();
  }
}

---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Evalution
its related to  autoResizeMode != AUTO_RESIZE_OFF (verify by
out-commenting the marked line). The column's width is set
correctly in the mouseDragged  in BasicTableHeaderUI. It's
resized back to the old value by table's autoResizing
efforts. By working back from the 1.3.1 code you can see
that the reason is a missing else block in
table.columnMarginChanged()

Workaround:
include the else block again - for the time being: subclass
JTable and override columnMarginChanged() as shown in the
commented section of the example

Release Regression From : 1.3.1_06
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Review ID: 179707) 
======================================================================

Comments
EVALUATION Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=14470
27-07-2006

EVALUATION Should investigate 4523678 along with this one. They may be related. ###@###.### 2003-07-08
08-07-2003