Solved

JTabbedPane

Posted on 2000-04-11
3
693 Views
Last Modified: 2008-03-10
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
Comment
Question by:psteph
  • 2
3 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2704780
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
 

Author Comment

by:psteph
ID: 2712466
Thanks it works great,
please lock the question.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 2712593
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 103
mapAB Challlenge 35 128
Free Alternative to JIRA 4 91
Oracle SQL syntax check  without executing 6 38
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now