Dynamically change jButton icon

后端 未结 5 1270
Happy的楠姐
Happy的楠姐 2021-01-19 22:14

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 perio

5条回答
  •  孤独总比滥情好
    2021-01-19 22:39

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

提交回复
热议问题