6

I have a jTabbedPane with multiple tabs in it. I am trying to make the title text of the selected/active tab bold. Is there a simple way to do this?

user2444472
  • 149
  • 1
  • 1
  • 4

3 Answers3

0
JTabbedPane pane = new JTabbedPane();
pane.addChangeListener(new ChangeListener(){

 @Override
 public void stateChanged(ChangeEvent e) {
     JTabbedPane source = (JTabbedPane) e.getSource();
     // Set all tabs to PLAIN font
     for(int i = 0; i < source.getTabCount(); i++) {
         Component c = source.getTabComponentAt(i);
         c.setFont(c.getFont().deriveFont(Font.PLAIN));
     }
     Component selectedComp = source.getTabComponentAt(source.getSelectedIndex());
     // Set selected component to BOLD
     selectedComp.setFont(selectedComp.getFont().deriveFont(Font.BOLD));
     }
});

Try this, i wrote it quickly, maybe you need to do some adjustments for the initial tab, don't know for sure.

Also not so sure if you need JTabbedPane.getTabComponentAt(int idx) or JTabbedPane.getComponentAt(int idx) although i suppose first version is correct.

Doru Chiulan
  • 307
  • 4
  • 15
  • **getComponentAt** gets the Component, ie the contents of the pane that the tab selects. It does not affect the title text on the tab. You can get the text of the title using **getTitleAt**, but you cannot change the font that way. – JavaLatte Mar 22 '16 at 14:02
0

I know that this question was asked a long time ago, but I recently wanted to do this as well. I found the answer here. The solution was one of two things:

  1. Use tabbedPane.setTitleAt(currSelextedIdx, "<html><b>My tab title</b></html>");
  2. Create your own UI implementation for the tabbed pane

I personally used the first option and set all the other tabs back to the regular tab title on a change. I also made the initial tab bold after initializing all the tabs (this can be done upon initialization).

Peter Kaufman
  • 752
  • 1
  • 6
  • 18
0

The simplest solution I've found until now. In a subclass of JTabbedPane, override two methods as below. When the tab is added, the raw title is persisted as a client property of the tab component. When the selected tab changes, the current tab title is restored to the raw title and the next tab title is set to bold with HTML.

public class MyTabbedPane extends JTabbedPane {

    ...
    
    @Override
    public void insertTab(String title, Icon icon, Component component, String tip, int index) {
        ((JComponent)component).putClientProperty("title", title);
        super.insertTab(title, icon, component, tip, index);
    }
    
    @Override
    public void setSelectedIndex(int index) {
        int currentIndex = getSelectedIndex();
        if (currentIndex >= 0) {
            JComponent previous = (JComponent) getComponentAt(currentIndex);
            String title = (String) previous.getClientProperty("title");
            setTitleAt(currentIndex, title);
        }
        super.setSelectedIndex(index);

        JComponent current = getSelectedComponent();
        String title = (String) current.getClientProperty("title");
        setTitleAt(index, "<html><b>" + title + "</b></html>");
    }

}
    
Inteli9
  • 16
  • 1
  • 2