求助:java swing中,自定义jtable中,如何改变指定某些行的颜色?最好每个方法都加上注释。
2011-08-25
展开全部
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class JTableTest extends JFrame{
private MyTable jtable;
private JScrollPane scrollPanel;
public JTableTest()
{
initComponent();
setComponentListener();
this.setMinimumSize(new Dimension(400,300));
this.setLocationRelativeTo(null);
}
private void setComponentListener()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initComponent()
{
Container content=this.getContentPane();
jtable=new MyTable(initRowData(),initColumnName());
scrollPanel=new JScrollPane(jtable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
content.add(scrollPanel);
}
private Vector<Vector<String>> initRowData()
{
Vector<Vector<String>> row=new Vector<Vector<String>>();
for(int i=0;i<10;i++)
{
Vector<String> column=new Vector<String>();
column.add("1");
column.add("2");
column.add("3");
row.add(column);
}
return row;
}
private Vector<String> initColumnName()
{
Vector<String> columnName=new Vector<String>();
columnName.add("test1");
columnName.add("test2");
columnName.add("test3");
return columnName;
}
public static void main(String[] args) {
new JTableTest().setVisible(true);
}
}
/*
* 自定义类MyJTable,调用时只需new MyTable并将行和列传入构造方法中即可
*/
class MyTable extends JTable
{
static int current=-1;
private Vector rowData;
public MyTable(Vector rowData,Vector columnName)
{
super(new DefaultTableModel( rowData,columnName));
this.rowData=rowData;
initComponent();
Exaggerate();
setComponentListener();
}
//初始化组件
private void initComponent()
{
//setAutoCreateRowSorter(true);// 调出三角块表格排序
//setAutoResizeMode(JTable.AUTO_RESIZE_OFF);// 添加表格下滚动条
//使表格表头的字体居中,若想居左居右,只要改变其属性
DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) this .getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
}
//调用渲染器
private void Exaggerate()
{
this.setDefaultRenderer(Object.class, new RoutineColor());
// for(int i=0;i<this.getColumnCount();i++)
// this.setDefaultRenderer(this.getColumnClass(i), new RoutineColor());
}
//设置组件侦听器
private void setComponentListener()
{
//给表格添加鼠标退出表格的监听,鼠标退出表格,使其颜色还原
this.addMouseListener(new MouseAdapter()
{
public void mouseExited(MouseEvent e) {
for(int i=0;i<rowData.size();i++)
{
if(i==current)
{
setBackground(Color.yellow);
break;
}
}
setBackground(Color.black);
current=-1;
}
}
);
//给表格添加鼠标移动的监听,鼠标移动刷新表格颜色
this.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e) {
Point p=e.getPoint();//首先获取鼠标坐标
current=MyTable.this.rowAtPoint(p);//根据坐标获取所在行
for(int i=0;i<rowData.size();i++)
{
if(i==current)
{
System.out.println(current);
MyTable.this.setBackground(Color.yellow);
break;
}
}
MyTable.this.setBackground(Color.black);
}
}
);
}
@Override
public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {
//设置单元格内容居中显示
DefaultTableCellRenderer tableRenderer=(DefaultTableCellRenderer)super.getDefaultRenderer(columnClass);
tableRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
return tableRenderer;
}
@Override
public boolean isCellEditable(int row, int column) {
//定义表格不可编辑的方法
return false;
}
}
/*
* 表格的渲染器
*/
class RoutineColor extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (row % 2 == 0) {
// 设置偶数行的背景颜色
setBackground(new Color(236, 246, 248));
} else {
// 基数行的背景颜色
setBackground(new Color(255, 255, 255));
}
if (row ==MyTable.current ) {
//鼠标悬浮行的颜色
setBackground(new Color(154,221,151));
}
/*继承Label类的方法, 设置table的单元格对齐方式*/
setHorizontalAlignment((int) Component.CENTER_ALIGNMENT); // 水平居中
setHorizontalTextPosition((int) Component.CENTER_ALIGNMENT); // 垂直居中
// table.getTableHeader().setBackground(new Color(206, 231, 255));//设置表头的字体色
table.setSelectionBackground(new Color(213, 235, 243));//设置选中行的背景色
table.setSelectionForeground(new Color(247, 81, 53));//设置选中行的前景色
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
//表格列根据内容调整大小
public static void adjustTableColumnWidths(JTable table) {
JTableHeader header = table.getTableHeader(); // 表头
int rowCount = table.getRowCount(); // 表格的行数
TableColumnModel cm = table.getColumnModel(); // 表格的列模型
for (int i = 0; i < cm.getColumnCount(); i++) { // 循环处理每一列
TableColumn column = cm.getColumn(i); // 第i个列对象
int width = (int) header.getDefaultRenderer()
.getTableCellRendererComponent(table,
column.getIdentifier(), false, false, -1, i)
.getPreferredSize().getWidth(); // 用表头的绘制器计算第i列表头的宽度
for (int row = 0; row < rowCount; row++) { // 循环处理第i列的每一行,用单元格绘制器计算第i列第row行的单元格度
int preferedWidth = (int) table.getCellRenderer(row, i)
.getTableCellRendererComponent(table,
table.getValueAt(row, i), false, false, row, i)
.getPreferredSize().getWidth();
width = Math.max(width, preferedWidth); // 取最大的宽度
}
column.setPreferredWidth(width + table.getIntercellSpacing().width); // 设置第i列的首选宽度
}
table.doLayout(); // 按照刚才设置的宽度重新布局各个列
}
}
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class JTableTest extends JFrame{
private MyTable jtable;
private JScrollPane scrollPanel;
public JTableTest()
{
initComponent();
setComponentListener();
this.setMinimumSize(new Dimension(400,300));
this.setLocationRelativeTo(null);
}
private void setComponentListener()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initComponent()
{
Container content=this.getContentPane();
jtable=new MyTable(initRowData(),initColumnName());
scrollPanel=new JScrollPane(jtable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
content.add(scrollPanel);
}
private Vector<Vector<String>> initRowData()
{
Vector<Vector<String>> row=new Vector<Vector<String>>();
for(int i=0;i<10;i++)
{
Vector<String> column=new Vector<String>();
column.add("1");
column.add("2");
column.add("3");
row.add(column);
}
return row;
}
private Vector<String> initColumnName()
{
Vector<String> columnName=new Vector<String>();
columnName.add("test1");
columnName.add("test2");
columnName.add("test3");
return columnName;
}
public static void main(String[] args) {
new JTableTest().setVisible(true);
}
}
/*
* 自定义类MyJTable,调用时只需new MyTable并将行和列传入构造方法中即可
*/
class MyTable extends JTable
{
static int current=-1;
private Vector rowData;
public MyTable(Vector rowData,Vector columnName)
{
super(new DefaultTableModel( rowData,columnName));
this.rowData=rowData;
initComponent();
Exaggerate();
setComponentListener();
}
//初始化组件
private void initComponent()
{
//setAutoCreateRowSorter(true);// 调出三角块表格排序
//setAutoResizeMode(JTable.AUTO_RESIZE_OFF);// 添加表格下滚动条
//使表格表头的字体居中,若想居左居右,只要改变其属性
DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) this .getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
}
//调用渲染器
private void Exaggerate()
{
this.setDefaultRenderer(Object.class, new RoutineColor());
// for(int i=0;i<this.getColumnCount();i++)
// this.setDefaultRenderer(this.getColumnClass(i), new RoutineColor());
}
//设置组件侦听器
private void setComponentListener()
{
//给表格添加鼠标退出表格的监听,鼠标退出表格,使其颜色还原
this.addMouseListener(new MouseAdapter()
{
public void mouseExited(MouseEvent e) {
for(int i=0;i<rowData.size();i++)
{
if(i==current)
{
setBackground(Color.yellow);
break;
}
}
setBackground(Color.black);
current=-1;
}
}
);
//给表格添加鼠标移动的监听,鼠标移动刷新表格颜色
this.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e) {
Point p=e.getPoint();//首先获取鼠标坐标
current=MyTable.this.rowAtPoint(p);//根据坐标获取所在行
for(int i=0;i<rowData.size();i++)
{
if(i==current)
{
System.out.println(current);
MyTable.this.setBackground(Color.yellow);
break;
}
}
MyTable.this.setBackground(Color.black);
}
}
);
}
@Override
public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {
//设置单元格内容居中显示
DefaultTableCellRenderer tableRenderer=(DefaultTableCellRenderer)super.getDefaultRenderer(columnClass);
tableRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
return tableRenderer;
}
@Override
public boolean isCellEditable(int row, int column) {
//定义表格不可编辑的方法
return false;
}
}
/*
* 表格的渲染器
*/
class RoutineColor extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (row % 2 == 0) {
// 设置偶数行的背景颜色
setBackground(new Color(236, 246, 248));
} else {
// 基数行的背景颜色
setBackground(new Color(255, 255, 255));
}
if (row ==MyTable.current ) {
//鼠标悬浮行的颜色
setBackground(new Color(154,221,151));
}
/*继承Label类的方法, 设置table的单元格对齐方式*/
setHorizontalAlignment((int) Component.CENTER_ALIGNMENT); // 水平居中
setHorizontalTextPosition((int) Component.CENTER_ALIGNMENT); // 垂直居中
// table.getTableHeader().setBackground(new Color(206, 231, 255));//设置表头的字体色
table.setSelectionBackground(new Color(213, 235, 243));//设置选中行的背景色
table.setSelectionForeground(new Color(247, 81, 53));//设置选中行的前景色
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
//表格列根据内容调整大小
public static void adjustTableColumnWidths(JTable table) {
JTableHeader header = table.getTableHeader(); // 表头
int rowCount = table.getRowCount(); // 表格的行数
TableColumnModel cm = table.getColumnModel(); // 表格的列模型
for (int i = 0; i < cm.getColumnCount(); i++) { // 循环处理每一列
TableColumn column = cm.getColumn(i); // 第i个列对象
int width = (int) header.getDefaultRenderer()
.getTableCellRendererComponent(table,
column.getIdentifier(), false, false, -1, i)
.getPreferredSize().getWidth(); // 用表头的绘制器计算第i列表头的宽度
for (int row = 0; row < rowCount; row++) { // 循环处理第i列的每一行,用单元格绘制器计算第i列第row行的单元格度
int preferedWidth = (int) table.getCellRenderer(row, i)
.getTableCellRendererComponent(table,
table.getValueAt(row, i), false, false, row, i)
.getPreferredSize().getWidth();
width = Math.max(width, preferedWidth); // 取最大的宽度
}
column.setPreferredWidth(width + table.getIntercellSpacing().width); // 设置第i列的首选宽度
}
table.doLayout(); // 按照刚才设置的宽度重新布局各个列
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询