Java JTable3 (四)

2014-11-24 07:45:53 · 作者: · 浏览: 5
;
setBounds(100, 100, 500, 375);
setLayout(new GridLayout(3,1));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane);
String[] columnNames = { "A", "B", "C", "D", "E", "F" };
Vector columnNameV = new Vector();
for (int column = 0; column < columnNames.length; column++) {
columnNameV.add(columnNames[column]);
}
Vector> tableva lueV = new Vector>();
for (int row = 1; row < 21; row++) {
Vector rowV = new Vector();
for (int column = 0; column < columnNames.length; column++) {
rowV.add(columnNames[column] + row);
}
tableva lueV.add(rowV);
}
final DefaultTableModel tableModel = new DefaultTableModel(tableva lueV,
columnNameV);
final JTable table = new MTable(tableModel);
table.setRowSorter(new TableRowSorter(tableModel));
scrollPane.setViewportView(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
TableColumnModel tableColumnModel = table.getColumnModel();
ListSelectionModel listSelectionModel = tableColumnModel
.getSelectionModel();
System.out.println(listSelectionModel.getSelectionMode());
System.out.println(table.getColumnSelectionAllowed());
System.out.println(table.getRowSelectionAllowed());
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
table.setBackground(Color.YELLOW);
table.setForeground(Color.RED);
table.setRowHeight(30); // in pixels

/* manipulate the table */
table.setRowSelectionInterval(1, 3);
table.setColumnSelectionInterval(1, 1);
table.addRowSelectionInterval(4, 5);// must consider the selection mode
// that has been set before
JPanel buttonPanel = new JPanel();

getContentPane().add(buttonPanel);
JButton selectAllButton = new JButton("全部选择");
selectAllButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
table.selectAll();
}
});
buttonPanel.add(selectAllButton);
JButton clearSelectionButton = new JButton("取消选择");
clearSelectionButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
table.clearSelection();
}
});
buttonPanel.add(clearSelectionButton);
System.out.println("表格共有" + table.getRowCount() + "行"
+ table.getColumnCount() + "列");
System.out.println("共有" + table.getSelectedRowCount() + "行被选中");
System.out.println("第1行的选择状态是: " + table.isRowSelected(0));
System.out.println("第3行的选择状态是: " + table.isRowSelected(2));
System.out.println("被选中的第一行索引是: " + table.getSelectedRow());
int[] selectedRows = table.getSelectedRows();
System.out.print("所有被选中的行的索引是: ");
for (int row = 0; row < selectedRows.length; row++) {
System.out.print(selectedRows[row] + "\t");
}
System.out.println();
System.out.println("移动前第2列的名称是: " + table.getColumnName(1));
System.out.println("移动前第2行第2列的值是: " + table.getValueAt(1, 1));
table.moveColumn(1, 3);
System.out.println("移动后第2列的名称是: " + table.getColumnName(1));
System.out.println("移动后第2行第2列的值是: " + table.getValueAt(1, 1));

/* 维护表格模型 */ www.2cto.com
JPanel panelModel = new JPanel();
panelModel.add(new JLabel("A: "));
final JTextField aTextField = n