Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change jButton icon

I have a program that detects when certain machines are online and creates a button with a green "online" icon to show this. I want to add the functionality to check periodically if this machine is still online, and if it's not, change the icon to the "offline" icon that I've already defined.

like image 729
Fallso Avatar asked Nov 01 '11 12:11

Fallso


1 Answers

I know how to set the icon, however I can't figure out a way to do it once the button has already been displayed

probably you have issues with Concurency in Swing, that means that all Swing code must be done on EDT

then you have to wrap myButton.setIcon(myIcon) to the invokeLater(), for example

SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        myButton.setIcon(myIcon);
    }
});
like image 186
mKorbel Avatar answered Oct 05 '22 12:10

mKorbel