How to change backgound color while getting focus

Hi Beni,

I have implemented CustomTitleFactory which implements DockTitleFactory and CustomTitle which extends AbstractDockTitle.
I also use the following code:

DockTitleVersion versionDock = titles.getVersion(SplitDockStation.TITLE_ID, null);
DockTitleFactory factoryDock = new CustomTitleFactory();
versionDock.setFactory(factoryDock, Priority.CLIENT);

But somehow I found after doing this, the background color of the title won’t change even if the dockable getting focus.
Is there a proper way to add this feature as default one?

Thank you!

AbstractDockTitle does not paint its background. You will have to do that yourself by overriding “paintBackground” (or by extending another title-class).

Have a look at the piece of code below, it is copied from the BasicDockTitle. The BasicDockTitle calls “isActive” to find out if the title is activated, and uses different colors to paint its background in such a case. (“active” returns true if the Dockable of the title has the focus).

    protected void paintBackground( Graphics g, JComponent component ) {
        Graphics2D g2 = (Graphics2D)g;

        if( gradient == null ){
        	if( isDisabled() ){
        		gradient = getGradient( disabledLeftColor.value(), disabledRightColor.value(), component );
        	}
        	else if ( isActive() ){
                gradient = getGradient( activeLeftColor.value(), activeRightColor.value(), component );
            }
            else{
                gradient = getGradient( inactiveLeftColor.value(), inactiveRightColor.value(), component );
            }
        }
        
        g2.setPaint( gradient );
        g.fillRect( 0, 0, component.getWidth(), component.getHeight() );
    }```

Thank you Beni, it is exactly what I am looking for.

Hi Beni,

I use the following code and the GUI looks OK. However, I just found out this one will cause infinite loop.
Do you have any idea what’s going on behind the scene?

{
     Graphics2D g2 = (Graphics2D) g;

     int width = component.getWidth();
     int height = component.getHeight();
     if (isActive())
     {             
         setFont(origFont.deriveFont(Font.BOLD));
         curPaint = new GradientPaint(0, 0, aColor.brighter(), 0, height, aColor.darker().darker());
     }
     else
     {
         setFont(origFont);
         curPaint = new GradientPaint(0, 0, Color.WHITE, 0, height, color);
     } 
     g2.setPaint(curPaint);
     g2.fillRect(0, 0, width, height);
};```

Thanks,
YC

I guess “setFont” will cause Swing to repaint the Component… “paint”-Methods are here for painting, and should not modify the Component.

I indeed checked JComponent’s setFont(), it will call repaint() only when the font is different than the old one. I am not sure it’s the main cause.
Anyway, I already have a workaround to deal with it, just curious what happened behind the scene.

Also, if I shouldn’t modify the component here, then what’s the proper way to make the title text bold if the current window is active?

Thanks!

Override the setActive method of the title, and set the font in there. The method is called every time when the active-state changes.