jTabbedPane set active tab text properties
Asked Answered
T

3

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?

Trigg answered 18/6, 2013 at 22:21 Comment(0)
P
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.

Pancreas answered 21/8, 2013 at 17:4 Comment(1)
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.Max
M
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).

Magistrate answered 8/3, 2020 at 14:17 Comment(0)
P
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>");
    }

}
    
Primm answered 7/1, 2022 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.