简体   繁体   中英

How to change background color of an edited cell in 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");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM