Jyoti
Jyoti

Reputation: 2115

how to change the color of text of tab on mouse over of JtabbedPane?

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

Upvotes: 3

Views: 2985

Answers (2)

jzd
jzd

Reputation: 23639

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"));

Upvotes: 3

Catalina Island
Catalina Island

Reputation: 7136

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

Upvotes: 0

Related Questions