重写表头渲染器:

package cn.ait.amfp.ui.util;

import java.awt.Color;
import java.awt.Component;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.UIResource;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;

/**
* @version 1.13 05/05/07
*/
public class DefaultTableCellHeaderRenderer extends DefaultTableCellRenderer
implements UIResource {
private boolean horizontalTextPositionSet;

public DefaultTableCellHeaderRenderer() {
setHorizontalAlignment(JLabel.CENTER);
}

public void setHorizontalTextPosition(int textPosition) {
horizontalTextPositionSet = true;
super.setHorizontalTextPosition(textPosition);
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Icon sortIcon = null;

boolean isPaintingForPrint = false;

if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
Color fgColor = null;
Color bgColor = null;
if (hasFocus) {
fgColor = UIManager
.getColor("TableHeader.focusCellForeground");
bgColor = UIManager
.getColor("TableHeader.focusCellBackground");
}
if (fgColor == null) {
fgColor = header.getForeground();
}
if (bgColor == null) {
bgColor = header.getBackground();
}
setForeground(fgColor);
setBackground(bgColor);

setFont(header.getFont());

isPaintingForPrint = header.isPaintingForPrint();
}

if (!isPaintingForPrint && table.getRowSorter() != null) {
if (!horizontalTextPositionSet) {
// There is a row sorter, and the developer hasn't
// set a text position, change to leading.
setHorizontalTextPosition(JLabel.LEADING);
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table
.getRowSorter().getSortKeys();
if (sortKeys.size() > 0
&& sortKeys.get(0).getColumn() == table
.convertColumnIndexToModel(column)) {
switch (sortKeys.get(0).getSortOrder()) {
case ASCENDING:
sortIcon = UIManager.getIcon("Table.ascendingSortIcon");
break;
case DESCENDING:
sortIcon = UIManager
.getIcon("Table.descendingSortIcon");
break;
case UNSORTED:
sortIcon = UIManager.getIcon("Table.naturalSortIcon");
break;
}
}
}
}

setText(value == null ? "" : value.toString());
setIcon(sortIcon);

Border border = null;
if (hasFocus) {
border = UIManager.getBorder("TableHeader.focusCellBorder");
}
if (border == null) {
border = UIManager.getBorder("TableHeader.cellBorder");
}
setBorder(border);

return this;
}
}


在表格中添加渲染器:

Jtable filenametable = new JTable();// 表格
DefaultTableCellHeaderRenderer headRender = new DefaultTableCellHeaderRenderer();//创建渲染器
headRender.setHorizontalAlignment(JLabel.CENTER);//表头居中
filenametable.getTableHeader().setDefaultRenderer(headRender);//添加渲染器
filenametable
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);//设置选择模式

更多相关文章

  1. java使用freemarker导出复杂的excel表格

随机推荐

  1. HTML哪些是块级元素,哪些是行内元素、
  2. 使用Semantic-UI垂直对齐div
  3. sublime3安装liveload,实现前端自动F5刷新
  4. TP-link 无线路由器WDS设置方法图解_无线
  5. 如何在刀片服务器的foreach循环中插入表
  6. 真人演示乳房按摩法 咪咪大而不垂
  7. 如何在动态创建的HTML标签内容中避免单引
  8. HTML+CSS实现审核流程步骤效果
  9. 使用下一个div中的文本设置“下一个”按
  10. 关注“飞翔的键盘” 近期PlayBook/QNX开