0

How can I remove OR change colour of the border surrounding these tabs?

ALSO, is it possible to have the tab text change colour when the mouse is hovering over it?

enter image description here

ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • Did you do some research? – Prasad Nov 15 '13 at 12:06
  • @Prasad Yes. I found this page but I don't understand which is which. http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingaJTabbedPaneLookandFeel.htm – ManInMoon Nov 15 '13 at 12:08
  • Ok you can change the color of tab text on hovering a mouse. **setForeground()** is the method that you can use. – Prasad Nov 15 '13 at 12:23

1 Answers1

1

Is it possible to have the tab text change colour when the mouse is hovering over it?

As stated in this answer you can set a custom component for rendering the tab title, through JTabbedPane.setTabComponentAt(int index, Component component) method. So you can do something like this:

final JTabbedPane tabbedPane = new JTabbedPane();

MouseListener mouseListener = new MouseAdapter() {            
    Color defaultColor;

    @Override
    public void mouseEntered(MouseEvent e) {
        JLabel label = (JLabel)e.getSource();
        defaultColor = label.getForeground();
        label.setForeground(Color.BLUE);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        JLabel label = (JLabel)e.getSource();
        label.setForeground(defaultColor);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        JLabel label = (JLabel)e.getSource();
        Point point = SwingUtilities.convertPoint(label, e.getPoint(), tabbedPane);
        int selectedTab = tabbedPane.getUI().tabForCoordinate(tabbedPane, point.x, point.y);
        switch(e.getButton()){
            case MouseEvent.BUTTON2: tabbedPane.removeTabAt(selectedTab); break;
                default: tabbedPane.setSelectedIndex(selectedTab);
        }                
    }

};

JLabel tab1 = new JLabel("Tab1");
tab1.addMouseListener(mouseListener);    

tabbedPane.addTab(null, new JPanel());
tabbedPane.setTabComponentAt(0, tab1);

How can I remove OR change colour of the border surrounding these tabs?

It's up to the Look and Feel decide the border color in this case. You should look into the L&F default properties and see if it's allowed change this color. For instance you could execute the following code to see L&F default properties (of course after setting the L&F):

for(Object key : UIManager.getLookAndFeelDefaults().keySet()){
    System.out.println(key + " = " + UIManager.get(key));
}
Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • So this link if not useful? http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingaJTabbedPaneLookandFeel.htm – ManInMoon Nov 15 '13 at 12:38
  • Not so much. I think these properties belong to Metal Look and Feel. If you're using another L&F the properties may change. @ManInMoon – dic19 Nov 15 '13 at 13:13
  • Please take a look to the example. I've noticed that if you click on the `JLabel` the default tab change is lost. So I've added the `mouseClicked` event process. If you press the second button (wheel in my case) then the tab is removed, otherwise the tab become selected. @ManInMoon – dic19 Nov 15 '13 at 13:37
  • I get an error on MouseAdapter on MouseListener mouseListener = new MouseAdapter(). Should this be a mouse listener? – ManInMoon Nov 15 '13 at 13:43
  • So should I change :MouseListener mouseListener = new MouseAdapter() to MouseAdapter mouseListener = new MouseAdapter() { ? OR have I got the wrong type of MouseListener? – ManInMoon Nov 15 '13 at 14:13
  • @ManInMoon I'll give it a try but I'd need to know what L&F do you use. If you could see my updated answer and post a dump of L&F properties then I could lead you in right way (at least I'll try so). – dic19 Nov 15 '13 at 18:36
  • It's a custom one developed by the platform I am writing my App for. Not sure how to view/open that – ManInMoon Nov 17 '13 at 14:17