Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inform the user that a specific Tab in a JTabbedPane needs attention?

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.

like image 291
David Avatar asked Aug 06 '11 08:08

David


People also ask

Which method is used to add tabs to a JTabbedPane?

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.

How do you switch tabs in JTabbedPane by clicking a button?

You should use the method JTabbedPane. setSelectedIndex(int index) with the index of the tab you want.

What is the role of tabbed pane?

The JTabbedPane class is used to switch between a group of components by clicking on a tab with a given title or icon.


2 Answers

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.

like image 63
Harry Joy Avatar answered Sep 28 '22 07:09

Harry Joy


Looking at http://download.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html you could use an icon to denote the tab that needs attention.

like image 21
Ivo Abeloos Avatar answered Sep 28 '22 09:09

Ivo Abeloos