开发者

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.

E开发者_Python百科dit: I am also interested in knowing how to force the focus on tab 2 if it ispossible.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜