How to change background color of an edited cell i

2019-02-26 06:29发布

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?

1条回答
放荡不羁爱自由
2楼-- · 2019-02-26 07:17

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

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

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;
}

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.

查看更多
登录 后发表回答