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、列大小调整模式
   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);//设置列的位置 从第二列转到第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相应双击展开事件

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 阅读(1101) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: