May
21
|
A simple approach would be to use a Swing Timer to repeatedly change the foreground and or background colours of the tab you wish to flash. The following code demonstrates this approach
import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTabbedPane; import javax.swing.Timer; public class FlashingTab extends JTabbedPane { private int _tabIndex; private Color _background; private Color _foreground; private Color _savedBackground; private Color _savedForeground; private Timer timer = new Timer(1000, new ActionListener() { private boolean on = false; @Override public void actionPerformed(ActionEvent e) { flash(on); on = !on; } }); public void flash(int tabIndex, Color foreground, Color background) { _tabIndex = tabIndex; _savedForeground = getForeground(); _savedBackground = getBackground(); _foreground = foreground; _background = background; timer.start(); } private void flash(boolean on) { if (on) { if (_foreground != null) { setForegroundAt(_tabIndex, _foreground); } if (_background != null) { setBackgroundAt(_tabIndex, _background); } } else { if (_savedForeground != null) { setForegroundAt(_tabIndex, _savedForeground); } if (_savedBackground != null) { setBackgroundAt(_tabIndex, _savedBackground); } } repaint(); } public void clearFlashing() { timer.stop(); setForegroundAt(_tabIndex, _savedForeground); setBackgroundAt(_tabIndex, _savedBackground); } public static void main(String[] args) { JFrame frame = new JFrame(); FlashingTab tabs = new FlashingTab(); tabs.addTab("ABC", new JLabel("Tab 1")); tabs.addTab("XYZ", new JLabel("Tab 2")); tabs.flash(1, Color.red, Color.yellow); frame.add(tabs); frame.pack(); frame.setVisible(true); } }
One Ping to “How to make a flashing tab in JTabbedPane?”
Leave a Reply
You must be logged in to post a comment.
May 28th, 2009 at 1:04 am
[…] an earlier question we showed how to make a single tab flash. Since then many people have asked if it is possible to have more than one tab […]