// 演示JTable功能
package Table;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
/**
*
* @author HuFeiyan
*/
public class JTableDemo extends JPanel {
JTable tableView;
JScrollPane scrollpane;
Dimension origin = new Dimension(0, 0);
JButton toUp;
String[] columnNames = {"C1", "C2", "C3", "C4", "C5"};
private final static int PREFERRED_WIDTH = 680;
private final static int PREFERRED_HEIGHT = 600;
private Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)},};
private boolean DEBUG = false;
public static void main(String[] args) {
JTableDemo t = new JTableDemo();
JFrame frame = new JFrame(JTableDemo.class.getName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(t, BorderLayout.CENTER);
t.setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public JTableDemo() {
init();
}
private void init() {
this.setLayout(new BorderLayout());
TableModel dataModel = new AbstractTableModel() {
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public boolean isCellEditable(int row, int col) {
// 返回false则单元格不可编辑, 前两列不能编辑
// 界面上拖动列的位置不影响列的编号
return col > 2;
}
@Override
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
};
tableView = new JTable(dataModel);
TableRowSorter sorter = new TableRowSorter(dataModel);
tableView.setRowSorter(sorter);
tableView.setRowHeight(2, 100);
scrollpane = new JScrollPane(tableView);
tableView.setFillsViewportHeight(true);
this.add(scrollpane, BorderLayout.CENTER);
toUp = new JButton("UP");
// 选中某一行,点击UP按钮上移
toUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int[] rowIndexs = JTableDemo.this.tableView.getSelectedRows();
if (rowIndexs == null || rowIndexs.length == 0) {
return;
}
int index = rowIndexs[0];
if (index != 0) {
Object[] temp = JTableDemo.this.data[index];
JTableDemo.this.data[index] = JTableDemo.this.data[index - 1];
JTableDemo.this.data[index - 1] = temp;
JTableDemo.this.tableView.setRowSelectionInterval(index - 1, index - 1);
for (int i = 1; i < rowIndexs.length; i++) {
JTableDemo.this.tableView.addRowSelectionInterval(rowIndexs[i], rowIndexs[i]);
}
}
printDebugData();
}
});
JPanel u = new JPanel();
final JCheckBox debug = new JCheckBox("DEBUG", DEBUG);
debug.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
DEBUG = debug.isSelected();
}
});
u.add(toUp);
u.add(debug);
this.add(u, BorderLayout.NORTH);
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}