如何更改JTable中的编辑的单元格的背景颜色? [英] How to change background color of an edited cell in JTable?

查看:678
本文介绍了如何更改JTable中的编辑的单元格的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处找,但我似乎仍不能找到一个回答我的问题。我读过所有关于小区渲染器和单元格编辑器,但仍然不知道...我有一个JTable,我想确保用户清楚地看到,他们正在编辑的单元格。默认情况下,在JTable中编辑的单元格得到一个较深的边界,但我想使背景的绿色。选择时,我可以把它的绿色,但只要我开始输入数据,绿色背景会消失,我正在写成白色细胞。

I searched everywhere but I still can't seem to find an answer to my question. I've read all about cell renderers and cell editors but still no idea... I have a JTable, and I want to make sure that the users clearly see which cell they are editing. by default, the edited cell in JTable gets a darker border, but i would like to make the background green. I can make it green when selected, but as soon as I start entering data, the green background disappears and I'm writing into a white cell.

能否请你帮我找到一种方法来保持绿色单元格的背景,即使在输入数据?

Could you please help me find a way to keep the background of a cell green even while entering data?

推荐答案

首先,获得表的默认选择背景色:

First, get the table's default selection background color:

Color color = UIManager.getColor("Table.selectionBackground");

二,重写<一个href=\"http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#$p$ppareEditor%28javax.swing.table.TableCellEditor,%20int,%20int%29\"相对=nofollow> prepareEditor() ,如本的例如,并设置编辑器组件的背景颜色相匹配:

Second, override prepareEditor(), as shown in this example, and set the background color of the editor component to match:

@Override
public Component prepareEditor(TableCellEditor editor, int row, int col) {
    Component c = super.prepareEditor(editor, row, col);
    c.setBackground(color);
    return c;
}

附录:在技术上是正确的,请注意,编辑器组件的颜色通常是由活跃的同时相应的UI委托管理。一个不幸的选择可能会导致对比度差和受损的可用性。目标看和放彻​​底的测试;感觉是必要的。

Addendum: While technically correct, note that the editor component's color is typically managed by the corresponding UI delegate while active. An unfortunate choice may result in poor contrast and impaired usability. Thorough testing on target Look & Feels is warranted.

这篇关于如何更改JTable中的编辑的单元格的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆