Say you have a user interface with five or more tabs, and you need to inform the user that tab "2" needs attention.
Is there a way to do this? For example make the tab flash orange, or change the colour of the tab? I have had no success with requestFocus.
Edit: I am also interested in knowing how to force the focus on tab 2 if it ispossible.
To create a tabbed pane, instantiate JTabbedPane , create the components you wish it to display, and then add the components to the tabbed pane using the addTab method.
You should use the method JTabbedPane. setSelectedIndex(int index) with the index of the tab you want.
The JTabbedPane class is used to switch between a group of components by clicking on a tab with a given title or icon.
You can achieve this by changing background and foreground of pane at position of tab using some timer. Just change it on some interval and it will look like its blinking. Here is a demo for this:
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTabbedPane pane = new JTabbedPane();
JPanel jPanel = new JPanel();
JButton button = new JButton("Blink tab");
jPanel.add(button);
pane.addTab("adsad", jPanel);
JPanel jPanel1 = new JPanel();
jPanel1.add(new JLabel("hi"));
pane.addTab("werqr", jPanel1);
final Color defaultBackColor = pane.getBackgroundAt(1); // default background color of tab
final Color defaultForeColor = pane.getForegroundAt(1); // default foreground color of tab
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Timer timer = new Timer(500, new ActionListener() {
boolean blinkFlag = false;
@Override
public void actionPerformed(ActionEvent e) {
blink(blinkFlag);
blinkFlag = !blinkFlag;
}
});
timer.start();
}
private void blink(boolean blinkFlag) {
if (blinkFlag) {
pane.setForegroundAt(1, Color.green);
pane.setBackgroundAt(1, Color.orange);
} else {
pane.setForegroundAt(1, defaultForeColor);
pane.setBackgroundAt(1, defaultBackColor);
}
pane.repaint();
}
});
frame.add(pane);
frame.setVisible(true);
Here 1
is the tab index which you want to blink. To stop blinking stop timer and set fore and background color to defaults.
I am also interested in knowing how to force the focus on tab 2 if it is possible.
If you want to transfer focus to that tab you can use setSelectedIndex(int index)
method.
Edit:-
As said by @perp in comment (also I have tested it and he is right) this will not work for look and feel other than WindowDefault. But the foreground color (text color) will still blink.
Looking at http://download.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html you could use an icon
to denote the tab that needs attention.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With