How do I inform users that a specific tab in jtabbedpane needs attention?

Suppose you have a user interface with five or more tabs and you need to notify the user that tab "2" needs attention

Is there any way to do this? For example, make the label flash orange, or change the color of the label? I didn't use requestfocus successfully

Editor: I'm also interested in knowing how to force attention to tab 2 if possible

Solution

You can do this by using a timer to change the background and foreground of the pane at the tab location Just change it within a certain time interval and it will flash This is a demonstration:

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 to blink Stop blinking, stop the timer and set the foreground and background colors to the default values

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

Edit:-

As @ perp said in the comments (I also tested it and he was right) this doesn't work for the look and feel outside of windowdefault But the foreground color (text color) still flashes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>