gpt4 book ai didi

java - 如何更改选定单元格的背景?

转载 作者:行者123 更新时间:2023-11-30 04:22:17 25 4
gpt4 key购买 nike

如何更改jTable中选定单元格的背景?

我已经编写了表格渲染,但它分配了所有行,并且看不到现在选择的单元格类型。

public class MyTableRanderer extends DefaultTableCellHeaderRenderer{

@Override
public Component getTableCellRendererComponent(JTable jtable, Object obj,
boolean isSelected, boolean hasFocus, int row, int col) {
setText(obj.toString());

if(isSelected){
setBackground(Color.ORANGE);
setForeground(Color.BLACK);
} else {
setBackground(Color.WHITE);
setForeground(Color.BLACK);
}

return this;
}

}

编辑:

public class MyTableRanderer extends DefaultTableCellHeaderRenderer {

@Override
public Component getTableCellRendererComponent(JTable jtable, Object obj,
boolean isSelected, boolean hasFocus, int row, int col) {
setText(obj.toString());

Component cell = super.getTableCellRendererComponent(
jtable, obj, isSelected, hasFocus, row, col);


if (isSelected) {
setBackground(Color.ORANGE);
cell.setBackground(Color.green);
setForeground(Color.BLACK);
} else {
setBackground(Color.WHITE);
setForeground(Color.BLACK);
}

return this;
}
}

最佳答案

How to change backgroung of selected cells in jTable?

I have already written table-rendering, but it allocates all row, and do not visible what kind of cell selected now

然后需要测试这两种方法if (isSelected & hasFocus) {

关于java - 如何更改选定单元格的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16751651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com