How to create a custom JTabbedPane to manage tab title text font colors

This source code shows how to create a custom JTabbedPane to help you set the foreground color of a tab’s title text:

/**
 * With Java 7 and OS X 10.9 (and maybe 10.10), there is no visual distinction between
 * a tab that's enabled and one that is disabled, so this is an effort to fix that.
 */
class CustomTabbedPane extends JTabbedPane {

    var tftTabIsEnabled = false

    override def getForegroundAt(index: Int): Color = {
        if (index == 1) {
            if (tftTabIsEnabled) {
                setToolTipTextAt(1, "")
                super.getForegroundAt(index)
            } else {
                setToolTipTextAt(1, "Tab is disabled until you connect to a database and select a template directory")
                Color.GRAY
            }
        } else {
            super.getForegroundAt(index)
        }
    }

}

In this example, I wanted the second tab (index = 1) to be disabled by default, so I wrote the code as shown, then used my CustomTabbedPane class instead of the JTabbedPane. Other code in my project handled the process of knowing whether the tab should be enabled or disabled, and called this code as necessary in that process. I deleted that code so I don’t remember exactly how it worked, but it called invalidate() or updateUI() on the tabbed pane whenever the state changed.

The code here is written in Scala, but as you can see, it transfers easily to Java.