JTable 的一个问题

yevone 2004-04-22 09:15:07
在用JTable时候,如何改变一个 单元格 的字体颜色啊,我是这么做的:

javax.swing.table.DefaultTableCellRenderer tce = (javax.swing.table.DefaultTableCellRenderer)jTable.getCellRenderer(3,3);
tce.setForeground(new Color(84,78,234));
jTable.setValueAt(ChangeCellValue,3,3);

这样子颜色是可以改变,但是只要我拉动一下滚动条,其他单元格的字体颜色也变了,这是怎么回事啊??请大家告诉我解决的办法啊!!!
...全文
135 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
yevone 2004-05-08
  • 打赏
  • 举报
回复
嗯多谢 xxlcg(☆突突☆)

也谢谢其他同志,揭帖了!!!
xxlcg 2004-04-27
  • 打赏
  • 举报
回复
首先(1,1)(2,2)里面放的必须是String的值,否则TableCellColorRenderer是不会起作用的。
new TableCellColorRenderer(this)看到参数“this”了吧?this就是当前类“myframe ”。
你在看看TableCellColorRenderer里面有个属性cellColorInterface,其实最终cellColorInterface=myframe,可以理解为cellColorInterface引用了myframe实例,那么
cellColorInterface.getColor(row,column,isSelected)就等价于
myframe.getColor(row,column,isSelected),能看明白吗?
RaiNnY1006 2004-04-27
  • 打赏
  • 举报
回复
学习中……
yevone 2004-04-26
  • 打赏
  • 举报
回复
to xxlcg(☆突突☆) :
我用了你的办法,还是不会变色 (1,1)(2,2)单元格还是为白色,我是不是忘了要调用什么方法吗?还有
jTable.setDefaultRenderer(String.class, new TableCellColorRenderer(this)); 这句设置了渲染后,在以后的程序并没有用到TableCellColorRenderer啊!能麻烦你说得清楚点吗?我刚开始学java ,周围的同学也不会,没人可以问,谢谢!
xxlcg 2004-04-25
  • 打赏
  • 举报
回复
使用接口CellColorInterface就是为了方便你设置表格格子颜色,你只要把你想要的颜色写在public Color getColor(int row, int col, boolean isSelected)里面,然后想改变的时候就jTable.updateUI();
yevone 2004-04-25
  • 打赏
  • 举报
回复
嗯多谢 xxlcg(☆突突☆) 又学了不少东西了,你用java多少年了啊??

再问一个问题如果我要用一个方法动态改变单元格的颜色我要怎么做啊难道要我每次 new 一个新
的frame吗?请赐教!
xxlcg 2004-04-23
  • 打赏
  • 举报
回复
报什么错?把frame的代码拈贴出来好吗?
yevone 2004-04-23
  • 打赏
  • 举报
回复
xxlcg(☆突突☆)
我的是一个frame 有一个table,我的frame已经implements implements ,但是就是不行啊,
yevone 2004-04-23
  • 打赏
  • 举报
回复
真是晕了啊,为什么我的总不行呢???
xxlcg 2004-04-23
  • 打赏
  • 举报
回复
在public Color getColor(int row, int col, boolean isSelected)的判断句后面再加上
return jTable.getBackground();
否则会编译不过^_^
xxlcg 2004-04-23
  • 打赏
  • 举报
回复
tblModel必须是jTable的模型。否则你就要get出jTable的模型来代替tblModel
xxlcg 2004-04-23
  • 打赏
  • 举报
回复
public class myframe extends JFrame implements CellColorInterface{
...................
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);
}
}
yevone 2004-04-23
  • 打赏
  • 举报
回复
xxlcg(☆突突☆) 在吗??上面是我的代码啊,帮忙看看好吗?
yevone 2004-04-23
  • 打赏
  • 举报
回复

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);
}
.......
}
yevone 2004-04-23
  • 打赏
  • 举报
回复
我在Cell里面放的是String 不是Label , 上面的extends JLabel 可以吗?


我还是些不出一个例子程序,
yevone 2004-04-22
  • 打赏
  • 举报
回复
这个两个class 是什么啊???

能否把思路告诉我一下
ralphvsclark 2004-04-22
  • 打赏
  • 举报
回复
用cell renderer
看我的例子吧。自己再改一下

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() : "");
}
}
yevone 2004-04-22
  • 打赏
  • 举报
回复
to iceandfire(【OPENSOURCE】) :

那个论坛我注册了,可是登入不了啊,还有什么资料啊,其他高手跑哪里去了???
iceandfire 2004-04-22
  • 打赏
  • 举报
回复
参考:
http://www2.opensource.com.cn

*********************************************
欢迎提供技术资料(译文,原创)
http://www2.opensource.com.cn/
*********************************************
yevone 2004-04-22
  • 打赏
  • 举报
回复
难道没有人知道吗???
加载更多回复(7)

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧