Java swing: selecting/deselecting JButton to imitate pulsing

后端 未结 2 687
轮回少年
轮回少年 2021-01-28 05:43

f.e. I have an email client, it receives new message, button with incoming messages starts doing something, until user clicks it to see whats up.

I\'m trying to make but

2条回答
  •  不知归路
    2021-01-28 06:21

    You should use Swing timers for that. Don't interact with GUI objects from foreign threads.

    There's some docs in the Java tutorial: How to use Swing timers.

    Here's an example way you could do this playing with the button's icon.

    // member var
    Icon buttonIcon;
    Timer timer;
    
      // in constructor for example
      buttonIcon = new ImageIcon("resources/icon.png");
      button.setIcon(buttonIcon);
    
      timer = new Timer(1000, this);
      timer.start();
    
       // in the actionPerformed handler
       if (button.getIcon() == null)
         button.setIcon(icon);
       else
         button.setIcon(null);
    

    Your class will need to implement ActionListener for this to work like that. Add some logic to stop the flashing when you need it.

提交回复
热议问题