3

I need to change the color of text of tab of JtabbedPane on MouseOver.

Is is possible using the Mouse Listener or is there any different property to do that?

Thanks Jyoti

Jyoti
  • 2,065
  • 3
  • 25
  • 28

2 Answers2

3

There is not a built-in property or method to do this.

One option is to create a custom JLabel (or other component) add a MouseListener that would change the color on mouse entry/exit.

Example, something like this:

class CustomMouseOverJLabel extends JLabel{
    public CustomMouseOverJLabel(String text) {
        super(text);
        addMouseListener(new MouseAdapter(){
            @Override
            public void mouseEntered(MouseEvent e) {
                setForeground(Color.BLUE);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                setForeground(Color.RED);
            }               
        });
    }       
}

Then when you make a call to addTab(title, item), also set custom title components like so:

yourTabbedPane.setTabComponentAt(index, new CustomMouseOverJLabel("title"));
jzd
  • 23,473
  • 9
  • 54
  • 76
0

The tabbed pane tutorial has an example of tabs with custom components that might help.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42