Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 704
  • Last Modified:

JTabbedPane

Hello,
I would like to set the text of a JTabbedPane to the following.
If tab selected = white
not selected = gray,
I have tried using the setForeground and setBackground methods but they have the tabbedpane as follows:
if tab selected = gray
not selected = white.
Thank you!
0
psteph
Asked:
psteph
  • 2
1 Solution
 
Jim CakalicSenior Developer/ArchitectCommented:
There don't appear to be any plaf resources that can be used to set the foreground or background properties of selected or unselected tabs. Too bad.

The best I can come up with is a ChangeListener that explicitly sets foreground colors on all tabs as in this example code. The steps are to identify (or create) a class that will implement ChangeListener. That class must implement the stateChanged method. Use JTabbedPane.addChangeListener() to make an object of this class a listener on your JTabbedPane. Don't forget to set the initial foregound color of your tabs or the initial display will use the look-and-feel defaults.

---------- TabbedPaneDemo.java ----------
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class TabbedPaneDemo implements ChangeListener {
    JTabbedPane pane;

    public TabbedPaneDemo(JFrame parent) {
        pane = new JTabbedPane();
        pane.add("First", new JLabel("First tab"));
        pane.add("Second", new JLabel("Second tab"));
        pane.add("Third", new JLabel("Third tab"));
        pane.add("Fourth", new JLabel("Fourth tab"));
        pane.add("Fifth", new JLabel("Fifth tab"));
        pane.addChangeListener((ChangeListener)this);
        this.stateChanged(null);
        parent.getContentPane().add(pane);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("JTabbedPane Demo");
        TabbedPaneDemo demo = new TabbedPaneDemo(frame);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setSize(400, 300);
        frame.show();
    }

    public void stateChanged(ChangeEvent e) {
        int count = pane.getTabCount();
        int selected = pane.getSelectedIndex();
        for (int i = 0; i < count; ++i) {
            Color c = (i == selected) ? Color.white : Color.gray;
            pane.setForegroundAt(i, c);
        }
    }

}
---------- end ----------

Its not particularly efficient but, with a small number of tabs, that probably won't be an issue. I haven't looked to see if the ChangeEvent has any useful information that could help make this algorithm more efficent -- say the previous selected tab and the currently selected tab? Or the class the implements ChangeListener might remember this and optimize accordingly. Regardless, this example works and should provide a foundation for implementing similar functionality in your code.

Best regards,
Jim Cakalic
0
 
pstephAuthor Commented:
Thanks it works great,
please lock the question.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
As a summary of the answer (to lock the question) -- use a ChangeListener that explicitly sets foreground colors on all tabs as in this example code. The steps are:

1) Identify (or create) a class that will implement ChangeListener. That class must implement the stateChanged method.

2) Implement a stateChanged method similar to:
    public void stateChanged(ChangeEvent e) {
        int count = pane.getTabCount();
        int selected = pane.getSelectedIndex();
        for (int i = 0; i < count; ++i) {
            Color c = (i == selected) ? Color.white : Color.gray;
            pane.setForegroundAt(i, c);
        }
    }

3) Use JTabbedPane.addChangeListener() to make an object of this class a listener on your JTabbedPane.

4) After constructing all the tabs and the containment heirarchy, set the initial foregound color of your tabs using the stateChanged method or similar code so that the initial display will not use the look-and-feel defaults.

Best regards,
Jim Cakalic
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now