package defineGUI;
import java.awt.Color;
import java.awt.Font;
import java.sql.Types;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
/**
* 自定义表格
* */
public class JDefineTable extends JTable {
/**
*
*/
private static final long serialVersionUID = 1L;
private DefaultTableModel data = new DefaultTableModel();
public JDefineTable(DefaultTableModel data) {
this.data = data;
}
/**
* 自定义表格
*
* @param data
* 表格数据源
* */
public JDefineTable() {
this.setModel(data);
this.setAutoCreateRowSorter(true);
this.setFont(new Font("楷体GB_2312", Font.BOLD, 16));
this.setRowHeight(30);
this.setForeground(Color.orange);
}
/**
* 创建表格字段名
* */
public void createDataColumns(String[] column) {
for (int i = 0; i < column.length; i++) {
data.addColumn(column[i]);
}
}
/**
* 向表中添加数据
*
* @param value
* String[]
* */
public void addRow(String[] value) {
this.getModel().addRow(value);
}
/**
* 向表中添加数据
*
* @param list
* List<String[]>
* */
public void addData(List<Object[]> list) {
for (int i = 0; i < list.size(); i++) {
this.addRow((String[]) list.get(i));
}
}
/**
* 向表中添加数据
*
* @param vector
* Vector<Object[]>
* */
public void addData(Vector<Object[]> vector) {
this.getModel().addRow(vector);
}
@Override
public DefaultTableModel getModel() {
return data;
}
/**
* 表格列值居中显示
*/
@Override
public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {
// 获得除表格头部分的单元格对象
DefaultTableCellRenderer tableRenderer = (DefaultTableCellRenderer) super
.getDefaultRenderer(columnClass);
// 设置单元格内容居中显示
tableRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
return tableRenderer;
}
/**
* 表格列名信息
*/
@Override
public JTableHeader getTableHeader() {
// 获得表格头对象
JTableHeader tableHeader = super.getTableHeader();
tableHeader.setReorderingAllowed(false);// 设置表格列不可重排
// 获得表格头的单元格对象
DefaultTableCellRenderer headerRenderer = (DefaultTableCellRenderer) tableHeader
.getDefaultRenderer();
// 设置单元格内容(即列名)居中显示
headerRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
return tableHeader;
}
/**
* 表格不可编辑
*/
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
/**
* 用来设置表格的选中行
*/
@Override
public void setRowSelectionInterval(int fromRow, int toRow) {
super.setRowSelectionInterval(fromRow, toRow);
}
/**
* 用来设置表格的唯一选中行 通过重载实现自己的方法
*
* @param row
*/
public void setRowSelectionInterval(int row) {
setRowSelectionInterval(row, row);
}
/**
* 表格行只可单选
*/
@Override
public ListSelectionModel getSelectionModel() {
ListSelectionModel selectionModel = super.getSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
return selectionModel;
}
@Override
public Class<?> getColumnClass(int column) {
int type=0;
/**ResultSetMetaData metaData = null;
try {
type = metaData.getColumnType(column + 1);
} catch (SQLException e) {
return super.getColumnClass(column);
}*/
switch (type) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return String.class;
case Types.BIT:
return Boolean.class;
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
return Integer.class;
case Types.BIGINT:
return Long.class;
case Types.FLOAT:
case Types.DOUBLE:
return Double.class;
case Types.DATE:
return java.sql.Date.class;
default:
return Object.class;
}
}
}
|
|