Friday, June 29, 2018

JTable background paint problem

I have a Java JTable with JFormattedTextField in it. I want to set the background color of text field to red when the value in it is not valid. Naturally, I added a focus listener to the text field and set the background color in focus lost event. During tests, I noticed that the background changes when I click on another text field in table but does not change when I press the tab key. The solution is to call JTable.repaint() after setting the background. I think the reason it works when I click on another text field is that during that click, JTable.repaint() is automatically called. Another adventure/nightmare in Java UI...

No comments: