Solved

JTabbedPane

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 156
JavaFX TableView not displaying correctly 3 109
Adding multiple JVM environments to RedHat 6 7 70
import as existing maven project 3 41
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

726 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