预览:
代码 :
/** * */ package com.han; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumnModel; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; /** * @author HAN * */ public class JTable3 extends JFrame { /** * coming from the JFrame */ private static final long serialVersionUID = -8022000459941822250L; /** * */ public JTable3() { // TODO Auto-generated constructor stub super(); setTitle("定义表格"); 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<String> columnNameV = new Vector<String>(); for (int column = 0; column < columnNames.length; column++) { columnNameV.add(columnNames[column]); } Vector<Vector<String>> tableValueV = new Vector<Vector<String>>(); for (int row = 1; row < 21; row++) { Vector<String> rowV = new Vector<String>(); for (int column = 0; column < columnNames.length; column++) { rowV.add(columnNames[column] + row); } tableValueV.add(rowV); } final DefaultTableModel tableModel = new DefaultTableModel(tableValueV, columnNameV); final JTable table = new MTable(tableModel); table.setRowSorter(new TableRowSorter<DefaultTableModel>(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)); /* 维护表格模型 */ JPanel panelModel = new JPanel(); panelModel.add(new JLabel("A: ")); final JTextField aTextField = new JTextField("A4", 10); panelModel.add(aTextField); panelModel.add(new JLabel("B: ")); final JTextField bTextField = new JTextField("B4", 10); panelModel.add(bTextField); JButton addButton = new JButton("添加"); panelModel.add(addButton); JButton updButton = new JButton("修改"); panelModel.add(updButton); JButton delButton = new JButton("删除"); panelModel.add(delButton); getContentPane().add(panelModel); table.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ int selectedRow=table.getSelectedRow(); selectedRow=table.convertRowIndexToModel(selectedRow);// because there exists sorting Object oa=tableModel.getValueAt(selectedRow, 0); Object ob=tableModel.getValueAt(selectedRow, 1); aTextField.setText(oa.toString()); bTextField.setText(ob.toString()); } }); addButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String[] rowValues = {aTextField.getText(), bTextField.getText()}; tableModel.addRow(rowValues); } }); updButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ int selectedRow=table.getSelectedRow(); selectedRow=table.convertRowIndexToModel(selectedRow); if(selectedRow != -1){ tableModel.setValueAt(aTextField.getText(), selectedRow, 0); tableModel.setValueAt(bTextField.getText(), selectedRow, 1); } } }); delButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ int selectedRow=table.getSelectedRow(); selectedRow=table.convertRowIndexToModel(selectedRow); if(selectedRow != -1){ tableModel.removeRow(selectedRow); } } }); } private class MTable extends JTable { /** * */ private static final long serialVersionUID = -3934300578737290495L; public MTable(TableModel tableModel) { // TODO Auto-generated constructor stub super(tableModel); } @Override public JTableHeader getTableHeader() { JTableHeader tableHeader = super.getTableHeader(); tableHeader.setReorderingAllowed(false); DefaultTableCellRenderer tableCellRenderer = (DefaultTableCellRenderer) tableHeader .getDefaultRenderer(); tableCellRenderer .setHorizontalAlignment(DefaultTableCellRenderer.CENTER); return tableHeader; } @Override public TableCellRenderer getDefaultRenderer(Class<?> columnClass) { DefaultTableCellRenderer cr = (DefaultTableCellRenderer) super .getDefaultRenderer(columnClass); cr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER); return cr; } @Override public boolean isCellEditable(int row, int column) { return false; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JTable3 frame = new JTable3(); frame.setVisible(true); } }