Solved

JTabbedPane

Posted on 2000-04-11
3
699 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

734 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