posts - 38,  comments - 22,  trackbacks - 0
          1、缺省編輯器
          class CustomModel extends DefaultTableModel {
          public CustomModel(Object[][] data, Object[] columnNames) {
          super(data, columnNames);
          }

          public Class getColumnClass(int col) {
          // dataVector is a protected member of DefaultTableModel

          Vector v 
          = (Vector)dataVector.elementAt(0);
          return v.elementAt(col).getClass();
          }

          public boolean isCellEditable(int row, int col) {
          Class columnClass 
          = getColumnClass(col);
          return columnClass != ImageIcon.class && 
          columnClass 
          != Date.class;
          }

          }
          2、列大小調(diào)整模式
             table.setAutoResizeMode(ex);
          JTable.AUTO_RESIZE_OFF,
          JTable.AUTO_RESIZE_NEXT_COLUMN,
          JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS,
          JTable.AUTO_RESIZE_LAST_COLUMN,
          JTable.AUTO_RESIZE_ALL_COLUMNS,
          3、列邊距
          table.getColumnModel().setColumnMargin(int );
          4、隱藏列

                  
          private JCheckBox checkBox = new JCheckBox("First Name Column Showing");

                  
          public ControlPanel() {
                      
          final TableColumnModel tcm = table.getColumnModel();
                      
          final TableColumn firstNameColumn = table.getColumn("First Name");

                      checkBox.setSelected(
          true);
                      add(checkBox);

                      checkBox.addActionListener(
          new ActionListener() {
                          
          public void actionPerformed(ActionEvent event) {
                              
          if (checkBox.isSelected()) {
                                  tcm.addColumn(firstNameColumn);
                                  tcm.moveColumn(
          20);//設(shè)置列的位置 從第二列轉(zhuǎn)到第0列
                              }
           else {
                                  tcm.removeColumn(firstNameColumn);
                              }

                              table.sizeColumnsToFit(
          -1);
                          }

                      }
          );
                  }

              

          5、鎖定左邊列

          public class 鎖定列 extends JFrame {
           Object[][] listings = new Object[][] {
             { "28 Pickelodan", "Mork and Mindy", "Dukes of Hazard",
               "I Love Lucy", "Andy Griffith", "Mission Impossible" },

             { "29 Dizey", "Rulan", "<-- Mulan", "<-- Mulan", "<-- Mulan",
               "<-- Mulan" },

             { "31 NBT", "Nightly News", "40/20", "<-- 40/20", "LimeTime",
               "<-- LimeTime" },

             { "32 AnimalUniverse", "Amazing Animals", "Animal Rescues",
               "Cute Animals", "Killer Animals", "Big and Small Animals" },

             { "34 DSPN", "Tuesday Night FootBall", "<--Tuesday Night FootBall",
               "<--Tuesday Night FootBall", "<--Tuesday Night FootBall",
               "<--Tuesday Night FootBall" },

             { "37 TLC", "Mind Mysteries", "Our World", "Ancient Wonders",
               "UFOs", "Ancient Inventions" },

             { "38 THC", "The Civil War", "Stalin", "Watergate", "Kent State",
               "WWII" }, };

           Object[] columnNames = new Object[] { "Channel", "7:30", "8:00", "8:30",
             "9:00", "9:30" };

           TableModel sharedModel = new DefaultTableModel(listings, columnNames);

           JTable table = new JTable(sharedModel), headerTable = new JTable(
             sharedModel);

           TableColumnModel tcm = table.getColumnModel();

           TableColumn firstColumn = tcm.getColumn(0);

           public 鎖定列() {
            Container cp = getContentPane();

            setActualPreferredColumnWidths(table);
            setActualPreferredColumnWidths(headerTable);

            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

            headerTable.getTableHeader().setReorderingAllowed(false);

            headerTable.setPreferredScrollableViewportSize(new Dimension(
              firstColumn.getPreferredWidth()
                + headerTable.getColumnModel().getColumnMargin(), 0));

            cp.add(new ControlPanel(), BorderLayout.NORTH);
            cp.add(new JScrollPane(table), BorderLayout.CENTER);
           }

           class ControlPanel extends JPanel {
            JCheckBox checkBox = new JCheckBox("First Column Locked");

            public ControlPanel() {
             add(checkBox);

             checkBox.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
               JScrollPane scrollPane = (JScrollPane) SwingUtilities
                 .getAncestorOfClass(JScrollPane.class, table);

               if (checkBox.isSelected()) {
                tcm.removeColumn(firstColumn);
                scrollPane.setRowHeaderView(headerTable);
                scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER,
                  headerTable.getTableHeader());
               } else {
                tcm.addColumn(firstColumn);

                int numCols = tcm.getColumnCount();
                tcm.moveColumn(numCols - 1, 0);
                scrollPane.setRowHeaderView(null);
               }
              }
             });
            }
           }

           public void setActualPreferredColumnWidths(JTable table) {
            int columnCount = table.getColumnCount();

            for (int i = 0; i < columnCount; ++i) {
             TableColumn c = table.getColumnModel().getColumn(i);
             int w = getActualPreferredColumnWidth(c);

             c.setPreferredWidth(w);
            }
           }

           public int getActualPreferredColumnWidth(TableColumn col) {
            int hw = columnHeaderWidth(col), // hw = header width
            cw = widestCellInColumn(col); // cw = column width

            return hw > cw ? hw : cw;
           }

           private int columnHeaderWidth(TableColumn col) {

            TableCellRenderer renderer = (col.getHeaderRenderer() == null) ? table
              .getTableHeader().getDefaultRenderer() : col
              .getHeaderRenderer();

            Component comp = renderer.getTableCellRendererComponent(table, col
              .getHeaderValue(), false, false, 0, 0);

            return comp.getPreferredSize().width;
           }

           private int widestCellInColumn(TableColumn col) {
            int c = col.getModelIndex(), width = 0, maxw = 0;

            for (int r = 0; r < table.getRowCount(); ++r) {
             TableCellRenderer renderer = table.getCellRenderer(r, c);

             Component comp = renderer.getTableCellRendererComponent(table,
               table.getValueAt(r, c), false, false, r, c);

             width = comp.getPreferredSize().width;
             maxw = width > maxw ? width : maxw;
            }
            return maxw;
           }

           public static void main(String args[]) {
            GraphicJavaApplication.launch(new 鎖定列(),
              "Locking the Left-Hand Column", 300, 300, 600, 210);
           }
          }


          6、選取模式
          table.setSelectionMode(
          selectionConstants[index]);
          int[] selectionConstants = {
          ListSelectionModel.SINGLE_SELECTION,
          ListSelectionModel.SINGLE_INTERVAL_SELECTION,
          ListSelectionModel.MULTIPLE_INTERVAL_SELECTION,
          };
          table.setColumnSelectionAllowed(boolean);

          table.setRowSelectionAllowed(boolean);

          table.setCellSelectionEnabled(boolean);

          7、JTable相應(yīng)雙擊展開(kāi)事件

          table.getTableHeader().addMouseListener(new ColumnFitAdapter());

          class ColumnFitAdapter extends MouseAdapter {
              
          public void mouseClicked(MouseEvent e) {
                  
          if (e.getClickCount() == 2{
                      JTableHeader header 
          = (JTableHeader) e.getSource();
                      TableColumn tableColumn 
          = getResizingColumn(header, e.getPoint());
                      
          if (tableColumn == null)
                          
          return;
                      
          int col = header.getColumnModel().getColumnIndex(
                              tableColumn.getIdentifier());
                      JTable table 
          = header.getTable();
                      
          int rowCount = table.getRowCount();
                      
          int width = (int) header.getDefaultRenderer()
                              .getTableCellRendererComponent(table,
                                      tableColumn.getIdentifier(), 
          falsefalse-1, col)
                              .getPreferredSize().getWidth();
                      
          for (int row = 0; row < rowCount; row++{
                          
          int preferedWidth = (int) table.getCellRenderer(row, col)
                                  .getTableCellRendererComponent(table,
                                          table.getValueAt(row, col), 
          falsefalse, row,
                                          col).getPreferredSize().getWidth();
                          width 
          = Math.max(width, preferedWidth);
                      }

                      header.setResizingColumn(tableColumn); 
          // this line is very important 
                      tableColumn.setWidth(width + table.getIntercellSpacing().width);
                  }

              }


              
          // copied from BasicTableHeader.MouseInputHandler.getResizingColumn 
              private TableColumn getResizingColumn(JTableHeader header, Point p) {
                  
          return getResizingColumn(header, p, header.columnAtPoint(p));
              }


              
          // copied from BasicTableHeader.MouseInputHandler.getResizingColumn 
              private TableColumn getResizingColumn(JTableHeader header, Point p,
                      
          int column) {
                  
          if (column == -1{
                      
          return null;
                  }

                  Rectangle r 
          = header.getHeaderRect(column);
                  r.grow(
          -30);
                  
          if (r.contains(p))
                      
          return null;
                  
          int midPoint = r.x + r.width / 2;
                  
          int columnIndex;
                  
          if (header.getComponentOrientation().isLeftToRight())
                      columnIndex 
          = (p.x < midPoint) ? column - 1 : column;
                  
          else
                      columnIndex 
          = (p.x < midPoint) ? column : column - 1;
                  
          if (columnIndex == -1)
                      
          return null;
                  
          return header.getColumnModel().getColumn(columnIndex);
              }

          }
          posted on 2007-04-11 10:10 aaabbb 閱讀(1105) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新蔡县| 任丘市| 合肥市| 子长县| 辽阳县| 唐山市| 云安县| 招远市| 台北市| 明光市| 防城港市| 赤峰市| 云安县| 静宁县| 永德县| 浦江县| 洮南市| 齐河县| 富顺县| 灵宝市| 广元市| 泸水县| 陕西省| 华宁县| 中牟县| 和林格尔县| 莆田市| 正定县| 芒康县| 霍邱县| 山东| 泽普县| 新密市| 武山县| 井冈山市| 海兴县| 赣州市| 工布江达县| 海城市| 淮阳县| 蓬安县|