jtable表头渲染器
来源:互联网 发布:matlab数据采集工具箱 编辑:程序博客网 时间:2024/04/30 10:04
重写表头渲染器:
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);//设置选择模式
- jtable表头渲染器
- jtable表头提示文本--通过重写表格头TableHeader的渲染器来实现
- JTable纵向表头,JTable表头纵向排列
- JTable中几种对象默认的渲染器
- 注意:JTable 渲染器同一列共享
- Jtable表头换行显示
- JTable的表头合并
- JTable的表头合并
- swing jtable表头设置
- JTable的表头合并
- JTable合并表头
- Jtable表头属性设置
- [Swing]JTable 表头显示问题
- java JTable表头自动换行
- JTable(swing)不显示表头
- JTable 表头和内容居中
- JTable中怎样显示表头
- 把JTable的表头去掉
- C语言中输入输出重定,freopen()妙用。
- Java面试问题和答案(三)
- N皇后问题
- js处理cookie详解
- 刚刚学习VC,发个程序试试
- jtable表头渲染器
- [leetcode][list][two pointers] Linked List Cycle II
- LVS DR模式中arp_ignore和arp_announce的作用解释
- html中onclick事件汇总
- Java面试问题和答案(四)
- Android开发--WIFI实现
- iOS - id && Object Messaging 深入讲解
- 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法
- 自适应算法和学习算法的区别