Solved

JTabbedPane

Posted on 2000-04-11
3
691 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java continue statement 10 72
Updating Java 9 87
count11 challenge 6 47
bitbucket vs gitbucket 3 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

708 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

16 Experts available now in Live!

Get 1:1 Help Now