Java JTable单元渲染器
我正在遵循我发现的一些代码(是的,我了解它是如何工作的) 从这里开始: 我要做的是,如果单元格值设置为“黄色”,则设置单元格的前景色 这是我的密码:Java JTable单元渲染器,java,swing,jtable,tablecellrenderer,Java,Swing,Jtable,Tablecellrenderer,我正在遵循我发现的一些代码(是的,我了解它是如何工作的) 从这里开始: 我要做的是,如果单元格值设置为“黄色”,则设置单元格的前景色 这是我的密码: public class Board extends JPanel{ private static final long serialVersionUID = 1L; int boardHeight = 20; int boardWidth = 10; JTable table; public Board() { table = n
public class Board extends JPanel{
private static final long serialVersionUID = 1L;
int boardHeight = 20;
int boardWidth = 10;
JTable table;
public Board() {
table = new JTable(this.boardHeight, this.boardWidth);
table.setDefaultRenderer(String.class, new BoardTableCellRenderer());
table.setFocusable(false);
table.setShowGrid(false);
table.setRowMargin(0);
table.setIntercellSpacing(new Dimension(0,0));
table.setRowSelectionAllowed(false);
table.setVisible(true);
this.add(table);
this.setPreferredSize(new Dimension(table.getPreferredSize().width, (table.getPreferredSize().height + 85)));
}
public void paint(Graphics g) {
table.setRowHeight(20);
for (int x = 0; x < this.table.getColumnCount(); ++x) {
TableColumn col = this.table.getColumnModel().getColumn(x);
col.setPreferredWidth(20);
}
}
}
问题是,如果我将任何单元格值设置为“黄色”,它不会改变
提前谢谢 添加这一行:
c.setOpaque(true);
GetTableCellRenderComponent返回的组件必须是不透明的,才能看到背景和前景颜色的变化。
这里的问题也是另一个:您正在扩展DefaultTableCellRenderer(这是一个JComponent),但返回的组件没有设置不透明方法。我会像这样重构您的代码:
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,int row,int col) {
String s = table.getModel().getValueAt(row, col).toString();
this.setOpaque(true);
if (s.equalsIgnoreCase("yellow")) {
this.setForeground(Color.YELLOW);
}
else {
this.setBackground(Color.WHITE);
}
return this;
}
你的渲染器曾经使用过吗?您将其作为包含字符串的单元格的默认呈现程序,但是您是否重载了模型的
getColumnClass
方法,以便它知道某些单元格包含字符串
因此,首先,我将使用println语句查看渲染器是否被调用,如果没有,我将覆盖模型的方法,如上所述
编辑1而且你的if结果一定会很奇怪。在“如果”部分中,你改变了前面的部分,而在“否则”部分中,你改变了背景——没有任何意义。您可能应该在if和else块中对状态进行补充更改,而不是正交更改 编辑2
例如:
import java.awt.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.*;
public class Board extends JPanel {
private static final long serialVersionUID = 1L;
int boardHeight = 20;
int boardWidth = 10;
JTable table;
Random random = new Random();
public Board() {
setLayout(new BorderLayout()); // !!
DefaultTableModel model = new DefaultTableModel(boardHeight, boardWidth) {
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
};
// !! table = new JTable(this.boardHeight, this.boardWidth);
table = new JTable(model);
for (int row = 0; row < model.getRowCount(); row++) {
for (int col = 0; col < model.getColumnCount(); col++) {
String s = random.nextBoolean() ? "red" : "yellow";
model.setValueAt(s, row, col);
}
}
table.setDefaultRenderer(String.class, new BoardTableCellRenderer());
table.setFocusable(false);
table.setShowGrid(false);
table.setRowMargin(0);
table.setIntercellSpacing(new Dimension(0, 0));
table.setRowSelectionAllowed(false);
table.setVisible(true);
this.add(table);
this.setPreferredSize(new Dimension(table.getPreferredSize().width,
(table.getPreferredSize().height + 85)));
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Board");
frame.getContentPane().add(new Board());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class BoardTableCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, col);
Object valueAt = table.getModel().getValueAt(row, col);
String s = "";
if (valueAt != null) {
s = valueAt.toString();
}
if (s.equalsIgnoreCase("yellow")) {
c.setForeground(Color.YELLOW);
c.setBackground(Color.gray);
} else {
c.setForeground(Color.black);
c.setBackground(Color.WHITE);
}
return c;
}
}
import java.awt.*;
导入java.util.Random;
导入javax.swing.*;
导入javax.swing.table.*;
公共课程委员会扩展JPanel{
私有静态最终长serialVersionUID=1L;
内板高度=20;
int boardWidth=10;
JTable表;
随机=新随机();
公共委员会(){
setLayout(新的BorderLayout());/!!
DefaultTableModel=新的DefaultTableModel(板高、板宽){
@凌驾
公共类getColumnClass(int columnIndex){
返回字符串.class;
}
};
//!!表格=新JTable(this.boardHeight,this.boardWidth);
表=新JTable(型号);
对于(int row=0;row
这里有一个简单的解决方案,使用TableCellRenderer作为内部类
myTable.setDefaultRenderer(Object.class, new TableCellRenderer()
{
JLabel comp = new JLabel();
String val;
@Override
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
comp.setOpaque(true);
comp.setForeground(Color.BLACK); // text color
if (value != null)
{
val = value.toString();
comp.setText(val);
if (val.equalsIgnoreCase("red"))
{
comp.setBackground(Color.RED);
}
else if (val.equalsIgnoreCase("yellow"))
{
comp.setBackground(Color.YELLOW);
}
else if (val.equalsIgnoreCase("green"))
{
comp.setBackground(Color.GREEN);
}
else
{
comp.setBackground(Color.WHITE);
}
}
return comp;
}
});
您不应该重写JPanel的paint方法,而应该重写它的paintComponent方法,不管怎样,您永远不应该从paint或paintComponent中调用程序逻辑。这表明您的代码需要过度拖拉。这是因为最终我将根据窗口大小使其拉伸,所以我需要它进行更新。这不是您使其拉伸的方式。同样,不要在这些方法中使用代码逻辑。您永远无法完全控制何时或是否调用此方法。如果您确实需要侦听调整大小事件(并且没有使用适当的布局管理器),那么您需要向JPanel添加ComponentListener。好的,我会记得稍后重新编码。谢谢请参阅我答案中的编辑1和编辑2。Eclipse告诉我它没有set不透明方法。而且它也不起作用。很抱歉让人恼火,但它仍然不起作用:/您的代码已被接受,但在我更改单元格值时颜色没有改变。
DefaultTableCellRenderer
默认是不透明的。这行不是这样做的:table.setDefaultRenderer(String.class,new BoardTableCellRenderer())代码>@Diesal11:不,一点也不。它只为字符串对象设置渲染器,但是模型无法知道什么是字符串单元格,除非您明确告诉它,这就是为什么您需要扩展模型,通常是DefaultTableModel。@Diesal:请参阅编辑,请阅读教程,因为大部分内容都在那里。我怀疑看不见的TableModel
没有返回相关列的String.class
。@trashgood:我同意。我怀疑OP也看不见未查看的表格模型
,并且从未被覆盖。
myTable.setDefaultRenderer(Object.class, new TableCellRenderer()
{
JLabel comp = new JLabel();
String val;
@Override
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
comp.setOpaque(true);
comp.setForeground(Color.BLACK); // text color
if (value != null)
{
val = value.toString();
comp.setText(val);
if (val.equalsIgnoreCase("red"))
{
comp.setBackground(Color.RED);
}
else if (val.equalsIgnoreCase("yellow"))
{
comp.setBackground(Color.YELLOW);
}
else if (val.equalsIgnoreCase("green"))
{
comp.setBackground(Color.GREEN);
}
else
{
comp.setBackground(Color.WHITE);
}
}
return comp;
}
});