Java中Table表头属性设置
/**
* 设置表单的表头高度
* @param table
*/
public static void setTableHeadHeight(JTable table, int height){
table.getTableHeader().setPreferredSize(new Dimension(1, height));
}
/**
* 设置表头的字体
* @param table
*/
public static void setTableHeadFont(JTable table, Font font){
table.getTableHeader().setFont(font);
}
/**
* 设置表头的背景颜色
* @param table
*/
public static void setTableHeadColor(JTable table, Color color){
table.getTableHeader().setBackground(color);
}
/**
* 设置表头文字的颜色
* @param table
* @param color
*/
public static void setTableHeadFontColor(JTable table, Color color){
table.getTableHeader().setForeground(color);
}
/**
* 设置表格的所有行的行高
* @param table
* @param height
*/
public void setTableAllRowHeight(JTable table, int height){
table.setRowHeight(height);
}
/**
* 设置表格的某一行的行高
* @param table
* @param row
* @param height
*/
public void setTableOneRowHeight(JTable table, int row, int height){
table.setRowHeight(row, height);
}
/**
* 设置列表某一列的宽度
* @param i
* @param width
*/
public static void setColumnSize(JTable table, int i, int preferedWidth, int maxWidth, int minWidth){
//表格的列模型
TableColumnModel cm = table.getColumnModel();
//得到第i个列对象
TableColumn column = cm.getColumn(i);
column.setPreferredWidth(preferedWidth);
column.setMaxWidth(maxWidth);
column.setMinWidth(minWidth);
}
/**
* 设置列表某几列的宽度
* @param table
* @param i
* @param preferedWidth
* @param maxWidth
* @param minWidth
*/
public static void setSomeColumnSize(JTable table, int[] i, int preferedWidth, int maxWidth, int minWidth){
TableColumnModel cm = table.getColumnModel();
if(i.length == 0){
return;
}
for(int j = 0; j < i.length; j++){
TableColumn column = cm.getColumn(i[j]);
column.setPreferredWidth(preferedWidth);
column.setMaxWidth(maxWidth);
column.setMinWidth(minWidth);
}
}
/**
* 表格数据居中
* @param table
*/
public void setTableColumnCenter(JTable table){
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, r);
}
/**
* 设置表格的某一行的背景色
* @param table
*/
public static void setOneRowBackgroundColor(JTable table, int rowIndex,
Color color) {
try {
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if (row == rowIndex) {
setBackground(color);
setForeground(Color.WHITE);
}else if(row > rowIndex){
setBackground(Color.BLACK);
setForeground(Color.WHITE);
}else{
setBackground(Color.BLACK);
setForeground(Color.WHITE);
}
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
}
};
int columnCount = table.getColumnCount();
for (int i = 0; i < columnCount; i++) {
table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 填充数据并排序后显示
* @param table
* @param tableHead
* @param data
*/
public static void changeAndSortTable(JTable table, Object[] tableHead, Object[][] data){
@SuppressWarnings("serial")
DefaultTableModel model = new DefaultTableModel(data, tableHead){
@SuppressWarnings({ "unchecked", "rawtypes" })
public Class getColumnClass(int column){
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
RowSorter sorter = new TableRowSorter(model);
table.setRowSorter(sorter);
}