开发者

How do I change the colour of an item on my gui if a value in the main program changes

Thanks for taking the time to try and help me- its really appreciated.

My question relates to changing the background colour of item on a GUI when a value in the main Java program changes. Basically my program is analysing external files and increments a int variable. When the variable changes I want the colour of an item (at the moment its just panels that I'm experimenting with but this might change).

I know how to get the item to change colour i.e. item.setBackground(Color.RED); but I don't know how to get that to happen when I want it to. In my head I know its something to do with listeners but everything I've ever been taught about listeners involves something happening on the actual GUI not the program itself.

Thanks again for reading and I hope you 开发者_如何学运维can help :)


Actually, so-called Observer pattern is not UI-specific. If you want your gui bean to be notified about changes in the main thread, it will fit fine. Take a look at this article if implemetation is unclear.

Note, that you have to call SwingUtilities.invokeLater() to update your GUI from the main thread.


Not at all. For the JVM, there's no special difference between the GUI classes and your "main" classes. You can write listeners anywhere. Take a look at this article, it will help you: http://download.oracle.com/javase/tutorial/uiswing/events/index.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜