参考:
http://www2.opensource.com.cn*********************************************
欢迎提供技术资料(译文,原创)
http://www2.opensource.com.cn/
*********************************************
http://www2.opensource.com.cn*********************************************
欢迎提供技术资料(译文,原创)
http://www2.opensource.com.cn/
*********************************************
解决方案 »
- 字节数组问题求教!
- Java拷贝图片文件出现错误-原始数据格式无法识别
- 关于变量的实例域问题
- 关于 类.class 与对象.getClass() 以及 Class.forName("类名");
- java如何遠程修改windows 2000 ip地址?
- 有关变量空间开辟 ,垃圾回收怎么回事?
- 有在中兴工作的朋友吗?中兴招java的吗?都做那方面的?
- 服务器端用Socket接受数据,bufferedreader的read方法读当数组当中。接收数据包时,有的数据包服务器端没有显示收到,这是为什么?
- 图像赋值???
- 关于数组成员变量在setter中的安全问题
- System.in的read函数每次读到回车就返回吗? 为什么?
- 如何使GUI界面在最小化的时候添加到OS的任务栏上!!!!(苦等)
看我的例子吧。自己再改一下import com.bradea.configeditor.bean.Bean;
import com.bradea.configeditor.table.model.CommonTableModel;
import java.awt.Color;
import java.awt.Component;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;public class CommonCellRenderer extends JLabel implements TableCellRenderer { /**
* The foreground color of deleted record.
*/
protected static final Color DELETED_CELL_FOREGROUND = Color.LIGHT_GRAY; /**
* The foreground color of modified record.
*/
protected static final Color MODIFIED_CELL_FOREGROUND = Color.RED; /**
* The foreground color when cell has focus.
*/
protected static final Color FOCUS_CELL_FOREGROUND = UIManager.getColor("Table.focusCellForeground"); /**
* The background color when cell has focus.
*/
protected static final Color FOCUS_CELL_BACKGROUND = UIManager.getColor("Table.focusCellBackground"); /**
* The border when the cell has focus.
*/
protected static final Border sHasFocusBorder = UIManager.getBorder("Table.focusCellHighlightBorder"); /**
* The border when the cell has no focus.
*/
protected static final Border sNoFocusBorder = new EmptyBorder(1, 1, 1, 1); /**
* This interface defines the method required by any object that would like
* to be a renderer for cells in a JTable.
*
* @param table The JTable that is asking the renderer to draw.
* @param value The value of the cell to be rendered.
* @param isSelected This interface defines the method required by any
* object that would like to be a renderer for cells in a JTable.
* @param hasFocus If true, render cell appropriately. For example, put a
* special border on the cell, if the cell can be edited, render in
* the color used to indicate editing.
* @param row The row index of the cell being drawn. When drawing the
* header, the value of row is -1.
* @param column The column index of the cell being drawn.
*/
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
setOpaque(true);
setFont(table.getFont());
setValue(value); // Set background color and foreground color when the cell is selected.
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
TableModel model = table.getModel();
List dataList = ((CommonTableModel) model).getDataList();
Bean tempBean = (Bean) dataList.get(row);
if (tempBean.isModified()) {
setForeground(MODIFIED_CELL_FOREGROUND);
} else if (tempBean.isDeleted()) {
setForeground(DELETED_CELL_FOREGROUND);
} else {
setForeground(table.getForeground());
}
setBackground(table.getBackground());
} // Set background color and foreground color when the cell has focus.
if (hasFocus) {
setBorder(sHasFocusBorder);
if (table.isCellEditable(row, column)) {
setForeground(FOCUS_CELL_FOREGROUND);
setBackground(FOCUS_CELL_BACKGROUND);
}
} else {
setBorder(sNoFocusBorder);
} return this;
} /**
* Set the text for the cell.
*
* @param value The value to be set to the cell.
*/
protected void setValue(Object value) {
setText(value != null ? value.toString() : "");
}
}
import javax.swing.table.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;/**
* <p>Title: 表格颜色渲染器</p>
* <p>Description: 实现表格不同格子显示不同颜色的功能。</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: PCI</p>
* @author 林楚光
* @version 1.0.0.6
*/class TableCellColorRenderer
extends JLabel
implements TableCellRenderer
{
CellColorInterface cellColorInterface;
/**
* 构造函数,引入CellColorInterface接口的实现实例。
* @param cellColorInterface CellColorInterface接口的实现实例
*/
public TableCellColorRenderer(CellColorInterface cellColorInterface)
{
super();
this.cellColorInterface=cellColorInterface;
setOpaque(true);
} /**每当表格的ui要update的时候,会调用本方法取得格子的渲染器。
* @param table :目标表格
* @param color :默认颜色
* @param isSelected :格子是否被选中
* @param hasFocus :各自是否正获得焦点
* @param row :格子所在的行
* @param column :个子所在的列
* @return :设置好的渲染器。
*/
public Component getTableCellRendererComponent(
JTable table, Object color,
boolean isSelected, boolean hasFocus,
int row, int column)
{
this.setBackground(cellColorInterface.getColor(row,column,isSelected));
this.setText(cellColorInterface.getText(row,column,isSelected));
return this;
}
}
接口文件:
import java.awt.*;/**
* <p>Title: 表格颜色接口类</p>
* <p>Description: 一个接口,实现表格中格子显示不同颜色的功能。</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: PCI</p>
* @author 林楚光
* @version 1.0.0.6
*/public interface CellColorInterface
{
/**
* 设置表格中每一格的底色。
* @param row 格子所在的行
* @param col 格子所在的列
* @param isSelected 是否被选中
* @return 格子希望的颜色
*/
public Color getColor(int row,int col,boolean isSelected);
/**
* 设置表格中每一格的内容。
* @param row 格子所在的行
* @param col 格子所在的列
* @param isSelected 是否被选中
* @return 格子希望的内容(String型)
*/
public String getText(int row,int col,boolean isSelected);
}
好像有点看懂了,具体怎么用啊
public class YourPanel extends JPanel implements CellColorInterface
{
........
void jbInit() throws Exception
{
.............
table.setDefaultRenderer(String.class, new TableCellColorRenderer(this));
}
/**
* 设置表格中每一格的内容。
* @param row 格子所在的行
* @param col 格子所在的列
* @param isSelected 是否被选中
* @return 格子希望的内容(String型)
*/
public String getText(int row, int col, boolean isSelected)
{
String value = (String)this.tableModel.getValueAt(row, col);
return value;
} /**
* 设置表格中每一格的底色。
* @param row 格子所在的行
* @param col 格子所在的列
* @param isSelected 是否被选中
* @return 格子希望的颜色
*/
public Color getColor(int row, int col, boolean isSelected)
{
Color color = this.table.getBackground();
Color disableColor = this.getBackground();
Color editDisAbleColor = Color.lightGray;
/*if (!tableModel.isCellEditable(row, col))
{
color = editDisAbleColor;
}*/
if ( ( (Vector) rowVector.get(row)).get(table.getColumnCount()).equals("-1"))
{
if (col < 2)
{
color = disableColor;
}
}
if (color != disableColor && isSelected)
{
color = table.getSelectionBackground();
}
return color;
}}
欢迎提供技术资料(原创、译文)
http://www2.opensource.com.cn/
*************************************
我的是一个frame 有一个table,我的frame已经implements implements ,但是就是不行啊,
我还是些不出一个例子程序,
To xxlcg(☆突突☆) :下面是我的部分代码,我想在init 里面把第 (1,1)(2,2) 单元格的背景色变成 red 我要怎么用你的class 啊?谢谢!!public class myframe
extends JFrame {
javax.swing.table.DefaultTableModel tblModel = null;
JTable jTable = null;
javax.swing.JScrollPane jsp1 = new JScrollPane();
public ExchangeRateFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(300, 530));
this.setTitle("Exchange Rate Window");
jsp1.setBounds(new Rectangle(0, 0, this.getWidth() - 5, 250));
jTable=new JTable(5,5);
this.jsp1.getViewport().add(jTable);
contentPane.add(jsp1, null);
}
.......
}
...................
private void jbInit() throws Exception {
.........
jTable.setDefaultRenderer(String.class, new TableCellColorRenderer(this));
}
public Color getColor(int row, int col, boolean isSelected)
{
if((row==1&&col==1)||(row==2&&col==2)){
return Color.red;
}
}
public String getText(int row, int col, boolean isSelected)
{
return (String)tblModel.getValueAt(row, col);
}
}
return jTable.getBackground();
否则会编译不过^_^
的frame吗?请赐教!
我用了你的办法,还是不会变色 (1,1)(2,2)单元格还是为白色,我是不是忘了要调用什么方法吗?还有
jTable.setDefaultRenderer(String.class, new TableCellColorRenderer(this)); 这句设置了渲染后,在以后的程序并没有用到TableCellColorRenderer啊!能麻烦你说得清楚点吗?我刚开始学java ,周围的同学也不会,没人可以问,谢谢!
new TableCellColorRenderer(this)看到参数“this”了吧?this就是当前类“myframe ”。
你在看看TableCellColorRenderer里面有个属性cellColorInterface,其实最终cellColorInterface=myframe,可以理解为cellColorInterface引用了myframe实例,那么
cellColorInterface.getColor(row,column,isSelected)就等价于
myframe.getColor(row,column,isSelected),能看明白吗?