View Javadoc

1   // %1126721452104:hoplugins.trainingExperience.ui%
2   package hoplugins.trainingExperience.ui;
3   
4   import javax.swing.JScrollPane;
5   import javax.swing.JTable;
6   import javax.swing.ListSelectionModel;
7   import javax.swing.table.TableColumnModel;
8   
9   
10  /***
11   * A Table with frozen columns
12   *
13   * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
14   */
15  public class TrainingRecapTable extends JScrollPane {
16      //~ Instance fields ----------------------------------------------------------------------------
17  
18      private JTable fixed;
19      private JTable scroll;
20  
21      //~ Constructors -------------------------------------------------------------------------------
22  
23      /***
24       * Creates a new TrainingRecapTable object.
25       *
26       * @param main Table to show
27       * @param fixedColumns number of fixed columns
28       */
29      public TrainingRecapTable(JTable main, int fixedColumns) {
30          super(main);
31          scroll = main;
32  
33          fixed = new JTable(scroll.getModel());
34          fixed.setFocusable(false);
35          fixed.setSelectionModel(scroll.getSelectionModel());
36          fixed.getTableHeader().setReorderingAllowed(false);
37          fixed.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); //$NON-NLS-1$
38          scroll.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); //$NON-NLS-1$
39  
40          //  Remove the fixed columns from the main table
41          for (int i = 0; i < fixedColumns; i++) {
42              TableColumnModel columnModel = scroll.getColumnModel();
43  
44              columnModel.removeColumn(columnModel.getColumn(0));
45          }
46  
47          scroll.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
48          fixed.setSelectionModel(scroll.getSelectionModel());
49  
50          //  Remove the non-fixed columns from the fixed table
51          while (fixed.getColumnCount() > fixedColumns) {
52              TableColumnModel columnModel = fixed.getColumnModel();
53  
54              columnModel.removeColumn(columnModel.getColumn(fixedColumns));
55          }
56  
57          fixed.getColumnModel().getColumn(0).setMaxWidth(120);
58          fixed.getColumnModel().getColumn(0).setMinWidth(120);
59          fixed.getColumnModel().getColumn(0).setWidth(120);
60  
61          //  Add the fixed table to the scroll pane
62          fixed.setPreferredScrollableViewportSize(fixed.getPreferredSize());
63          setRowHeaderView(fixed);
64          setCorner(JScrollPane.UPPER_LEFT_CORNER, fixed.getTableHeader());
65      }
66  
67      //~ Methods ------------------------------------------------------------------------------------
68  
69      /***
70       * Returns the Locked LeftTable
71       *
72       * @return Jtable
73       */
74      public JTable getLockedTable() {
75          return fixed;
76      }
77  
78      /***
79       * Returns the Scrollable RightTable
80       *
81       * @return Jtable
82       */
83      public JTable getScrollTable() {
84          return scroll;
85      }
86  }