How do I tell the user that a specific tab in JTabbedPane needs attention?

Say that you have a user interface with five or more tabs, and you need to tell the user that the “2” tab needs attention.

Is there any way to do this? For example, make the tab orange or change the color of the tab? I have not been successful with requestFocus.

Edit: I'm also interested in learning how to get focus on tab 2, if possible.

+6
source share
2 answers

You can achieve this by changing the background and foreground of the panel at the tab position with some timer. Just change it to a certain 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 you want to blink. To stop the flashing stop timer and set the foreground and background color to default.


I am also interested in learning how to set focus on tab 2, if possible.

If you want to shift focus to this tab, you can use the setSelectedIndex(int index) method.


Edit: -

As @perp said in a comment (I also tested it, and he's right), it will not look different than WindowDefault. But the foreground color (text color) will still blink.

+6
source

By looking at http://download.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html , you can use the icon to indicate the tab that needs attention.

+2
source

Source: https://habr.com/ru/post/894433/


All Articles