Swing - Creating mouse rollover events on tab selectors in Swing

Hi,

I have a jTabbedPane on which I have placed several panels to make tabs.

Here are the two problems.

1.How can I control the background color of the tab selector(where the title of the tab resides) when its tab is selected. I have been able to set dynamically the unselected ones.

2. How can I change the color of the text in the title of the tab when the mouse rollovers the tab selector. When a mouse goes over the selector, the text in the selector needs to change to blue. When the mouse leaves the selector the text color needs to go back to black. I need to capture the event for the selector part only.

I have been able to do it with buttons but how can I do it with tab selectors?

Thanks for your help.

[782 byte] By [Dean_Murraya] at [2007-11-26 23:23:48]
# 1

If you have been able to make a JButton do what you are look to do, then I think you might be able to just use a JButton as the tab selector. The code related to this is in the tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html

The TabbedComponentDemo is the one that gave my the idea since it has normal JButton listeners. You could just get rid of the label they use in the example and have your JButton be the entire selector.

If you are set on your current approach. Post some example code, and we'll try to debug.

pthorsona at 2007-7-10 14:29:57 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for the quick reply. I will try it out next week and let you know how I made out.
Dean_Murraya at 2007-7-10 14:29:57 > top of Java-index,Desktop,Core GUI APIs...
# 3

If you have been able to make a JButton do what you

are look to do, then I think you might be able to

just use a JButton as the tab selector. The code

related to this is in the tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/compon

ents/tabbedpane.html

The TabbedComponentDemo is the one that gave my the

idea since it has normal JButton listeners. You could

just get rid of the label they use in the example and

have your JButton be the entire selector.

If you are set on your current approach. Post some

example code, and we'll try to debug.

I checked it out but it is only available in 1.6. Is there a way to do this in 1.5?

Dean_Murraya at 2007-7-10 14:29:57 > top of Java-index,Desktop,Core GUI APIs...
# 4

I just did this quick but hopefully it should help you with the mouse events, simply get the rectangles that are associated with the tabs, and dectect when the mouse is inside of one, or not inside of one

public class TabbedPaneExample {

JTabbedPane tabbedPane;

Rectangle r[];

public TabbedPaneExample() {

tabbedPane = new JTabbedPane();

tabbedPane.addTab("PANEL1", new JPanel());

tabbedPane.addTab("PANEL2", new JPanel());

tabbedPane.addTab("PANEL3", new JPanel());

JFrame frame = new JFrame("TabbedExample");

frame.setSize(400, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(tabbedPane);

frame.setVisible(true);

//Get the rectangles associated with each tab

TabbedPaneUI ui = tabbedPane.getUI();

r = new Rectangle[3];

for (int j = 0; j < 3; j++)

r[j] = ui.getTabBounds(tabbedPane, j);

//add a MouseMotionListener to detect when a mouse enters a tab

tabbedPane.addMouseMotionListener(

new MouseMotionAdapter() {

public void mouseMoved(MouseEvent e) {

for (int j = 0; j < 3; j++)

if (r[j].contains(e.getPoint()))

{

//put your code for changes here

System.out.println("inside a tab");

}

}

}

);

}

public static void main(String args[]) {

TabbedPaneExample tp = new TabbedPaneExample();

}

}

Hope that helps

Octaclota at 2007-7-10 14:29:57 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks. That helped me do what I wanted to do using Java 1.5.
Dean_Murraya at 2007-7-10 14:29:57 > top of Java-index,Desktop,Core GUI APIs...