/[classpath]/classpath/javax/swing/JTable.java
ViewVC logotype

Diff of /classpath/javax/swing/JTable.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.44 by abalkiss, Wed Aug 31 18:54:47 2005 UTC revision 1.45 by rabbit78, Mon Sep 12 12:31:40 2005 UTC
# Line 47  import java.awt.event.ActionEvent; Line 47  import java.awt.event.ActionEvent;
47  import java.awt.event.ActionListener;  import java.awt.event.ActionListener;
48  import java.awt.event.KeyAdapter;  import java.awt.event.KeyAdapter;
49  import java.awt.event.KeyEvent;  import java.awt.event.KeyEvent;
50    import java.beans.PropertyChangeEvent;
51    import java.beans.PropertyChangeListener;
52  import java.text.DateFormat;  import java.text.DateFormat;
53  import java.text.NumberFormat;  import java.text.NumberFormat;
54  import java.util.Date;  import java.util.Date;
# Line 80  public class JTable extends JComponent Line 82  public class JTable extends JComponent
82    implements TableModelListener, Scrollable, TableColumnModelListener,    implements TableModelListener, Scrollable, TableColumnModelListener,
83               ListSelectionListener, CellEditorListener, Accessible               ListSelectionListener, CellEditorListener, Accessible
84  {  {
85      /**
86       * Handles property changes from the <code>TableColumn</code>s of this
87       * <code>JTable</code>.
88       *
89       * More specifically, this triggers a {@link #revalidate} call if the
90       * preferredWidth of one of the observed columns changes.
91       */
92      class TableColumnPropertyChangeHandler implements PropertyChangeListener
93      {
94        /**
95         * Receives notification that a property of the observed TableColumns
96         * has changed.
97         *
98         * @param ev the property change event
99         */
100        public void propertyChange(PropertyChangeEvent ev)
101        {
102          if (ev.getPropertyName().equals("preferredWidth"))
103            {
104              JTableHeader header = getTableHeader();
105              TableColumn col = (TableColumn) ev.getSource();
106              header.setResizingColumn(col);
107              doLayout();
108              header.setResizingColumn(null);
109            }
110        }
111      }
112    
113    /**    /**
114     * A cell renderer for boolean values.     * A cell renderer for boolean values.
# Line 584  public class JTable extends JComponent Line 613  public class JTable extends JComponent
613    Object oldCellValue;    Object oldCellValue;
614    
615    /**    /**
616       * The property handler for this table's columns.
617       */
618      TableColumnPropertyChangeHandler tableColumnPropertyChangeHandler =
619        new TableColumnPropertyChangeHandler();
620    
621      /**
622     * Creates a new <code>JTable</code> instance.     * Creates a new <code>JTable</code> instance.
623     */     */
624    public JTable ()    public JTable ()
# Line 645  public class JTable extends JComponent Line 680  public class JTable extends JComponent
680    {    {
681      setModel(dm == null ? createDefaultDataModel() : dm);      setModel(dm == null ? createDefaultDataModel() : dm);
682      setSelectionModel(sm == null ? createDefaultSelectionModel() : sm);      setSelectionModel(sm == null ? createDefaultSelectionModel() : sm);
683        
684      this.columnModel = cm;      if (cm != null)
685          {
686            setColumnModel(cm);
687            setAutoCreateColumnsFromModel(false);
688          }
689        else
690          {
691            setColumnModel(createDefaultColumnModel());
692            setAutoCreateColumnsFromModel(true);
693          }
694      initializeLocalVars();      initializeLocalVars();
695      // The next two lines are for compliance with the JDK which starts      // The next two lines are for compliance with the JDK which starts
696      // the JLists associated with a JTable  with both lead selection      // the JLists associated with a JTable  with both lead selection
# Line 659  public class JTable extends JComponent Line 703  public class JTable extends JComponent
703    protected void initializeLocalVars()    protected void initializeLocalVars()
704    {    {
705      setTableHeader(createDefaultTableHeader());      setTableHeader(createDefaultTableHeader());
706      this.autoCreateColumnsFromModel = false;      if (autoCreateColumnsFromModel)
707      if (columnModel == null)        createDefaultColumnsFromModel();
       {  
         this.autoCreateColumnsFromModel = true;  
         createColumnsFromModel();  
       }  
708      this.columnModel.addColumnModelListener(this);      this.columnModel.addColumnModelListener(this);
709            
710      this.defaultRenderersByColumnClass = new Hashtable();      this.defaultRenderersByColumnClass = new Hashtable();
# Line 754  public class JTable extends JComponent Line 794  public class JTable extends JComponent
794        }        }
795            
796      columnModel.addColumn(column);      columnModel.addColumn(column);
797        column.addPropertyChangeListener(tableColumnPropertyChangeHandler);
798    }    }
799    
800    protected void createDefaultEditors()    protected void createDefaultEditors()
# Line 799  public class JTable extends JComponent Line 840  public class JTable extends JComponent
840      return new JTableHeader(columnModel);      return new JTableHeader(columnModel);
841    }    }
842    
   private void createColumnsFromModel()  
   {  
     if (dataModel == null)  
       return;  
   
     TableColumnModel cm = createDefaultColumnModel();  
   
     for (int i = 0; i < dataModel.getColumnCount(); ++i)  
       {  
         cm.addColumn(new TableColumn(i));  
       }  
     this.setColumnModel(cm);  
   }  
   
843    // listener support    // listener support
844    
845    public void columnAdded (TableColumnModelEvent event)    public void columnAdded (TableColumnModelEvent event)
# Line 890  public class JTable extends JComponent Line 917  public class JTable extends JComponent
917      if ((event.getFirstRow() ==TableModelEvent.HEADER_ROW)      if ((event.getFirstRow() ==TableModelEvent.HEADER_ROW)
918          && autoCreateColumnsFromModel)          && autoCreateColumnsFromModel)
919    
920          createColumnsFromModel();          createDefaultColumnsFromModel();
921    
922      repaint();      repaint();
923    }    }
# Line 1663  public class JTable extends JComponent Line 1690  public class JTable extends JComponent
1690            
1691      if (m != null)      if (m != null)
1692        {        {
1693          // Set property.          // Set property.
1694          dataModel = m;          dataModel = m;
1695    
1696          // Add table as TableModelListener to new model.          // Add table as TableModelListener to new model.
1697          dataModel.addTableModelListener(this);          dataModel.addTableModelListener(this);
1698    
1699          // Automatically create columns.          // Automatically create columns.
1700          if (autoCreateColumnsFromModel)          if (autoCreateColumnsFromModel)
1701            createColumnsFromModel();            createDefaultColumnsFromModel();
1702        }        }
1703            
1704      // Repaint table.      // Repaint table.
# Line 2023  public class JTable extends JComponent Line 2050  public class JTable extends JComponent
2050    
2051            case AUTO_RESIZE_OFF:            case AUTO_RESIZE_OFF:
2052            default:            default:
2053                int prefWidth = resizingColumn.getPreferredWidth();
2054                resizingColumn.setWidth(prefWidth);
2055            }            }
2056        }        }
2057      else      else
# Line 2258  public class JTable extends JComponent Line 2287  public class JTable extends JComponent
2287     */     */
2288    public void createDefaultColumnsFromModel()    public void createDefaultColumnsFromModel()
2289    {    {
2290        assert columnModel != null : "The columnModel must not be null.";
2291    
2292      // remove existing columns      // remove existing columns
2293      int columnIndex = columnModel.getColumnCount() - 1;      int columnIndex = columnModel.getColumnCount() - 1;
2294      while (columnIndex >= 0)      while (columnIndex >= 0)
# Line 2273  public class JTable extends JComponent Line 2304  public class JTable extends JComponent
2304        TableColumn column = new TableColumn(c);        TableColumn column = new TableColumn(c);
2305        column.setIdentifier(dataModel.getColumnName(c));        column.setIdentifier(dataModel.getColumnName(c));
2306        columnModel.addColumn(column);        columnModel.addColumn(column);
2307          column.addPropertyChangeListener(tableColumnPropertyChangeHandler);
2308      }      }
2309    }    }
2310    

Legend:
Removed from v.1.44  
changed lines
  Added in v.1.45

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26