2

I am using a JTabbedPane. In this i have to disable some tabs. i disable the tab using setEnabledAt(index,false) method. the tab was actually disabled but i don't get look and feel [the color of the disabled tab text become light color]. how do i rectify it?


First i use default look and feel of java... now i change the look and feel to nimbus.. i found the color changes but the disabled tab was not look like disabled checkbox or radio button or other component .

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Siddhu
  • 1,188
  • 2
  • 14
  • 24

4 Answers4

2

I presume what you really want is to disable all the components on the disabled tab?

Swing doesn't work the way that setEnabled() of a parent component has any effect on the child components - you need to call setEnabled(false) on each and every one of them yourself (after all you could want some of them enabled, so Swing makes no assumptions and leaves it completely to you).

Durandal
  • 19,919
  • 4
  • 36
  • 70
1

I wasn't aware that there was supposed to be a change of the LookAndFeel, but using the UIManager.put, you're able to alter the LookAndFeel of very particular parts, and I bet by manually changing the look/feel of a disabled tab, you may be able to get what you're after.

You can look at this page for a bit of help, and a link to all the look/feel options http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/color.html

1
  • most of method for JTabbedPane is protected in the API, and not accesible from Swing methods

  • have to look for Custom XxxTabbedPaneUI, override these methods, and could be accesible from outside

  • correct way would be to implement Custom Look & Feel only, part of them override JTabbedPane with protected methods too

  • example for Custom XxxTabbedPaneUI

  • you can to change Foreground for disabled tab quite easily, but works correctly only by using HTML syntax

code

tabbedPane.setTitleAt(2, "<html><font color="
    + (tabbedPane.isEnabledAt(int tab) ? "black" : "red") + ">"
    + tabbedPane.getTitleAt(int tab) + "</font></html>");
  • I'd to suggest use custom Look and Feel or simple to change Foreground by using HTML
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

Perhaps you have an own Tab-Component?

Then you have to delegate the Enable-State to this Component.

import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class Display extends JFrame {

    public Display() {
        super("Enable");
        this.setDefaultCloseOperation( EXIT_ON_CLOSE );
        this.setSize( new Dimension(300,300) );
        this.setLocationRelativeTo( null );

        JTabbedPane tabs = new JTabbedPane() {

            // delegating to tabComponent 
            @Override
            public void setEnabledAt( int index, boolean enabled ) {
                super.setEnabledAt( index, enabled );
                Component tabComponent = getTabComponentAt( index );
                if(tabComponent != null) {
                    getTabComponentAt( index ).setEnabled( enabled );
                }
            }
        };  

        JComponent component = new JLabel("Lorem");
        tabs.addTab( "", component );
        JLabel custom = new JLabel("disabled");
        tabs.setTabComponentAt( tabs.indexOfComponent( component ), custom );

        tabs.addTab( "enabled", new JLabel("Ipsum") );
        tabs.addTab( "disabled", new JLabel("Dolor") );
        tabs.addTab( "enabled", new JLabel("Sit") );
        tabs.addTab( "enabled", new JLabel("Amet") );
        tabs.setEnabledAt( 0, false );
        tabs.setEnabledAt( 2, false );
        tabs.setSelectedIndex( 1 );
        this.getContentPane().add( tabs );
    }

    public static void main( String[] args ) throws Exception {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Windows".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
        SwingUtilities.invokeLater( new Runnable() {
            @Override
            public void run() {
                new Display().setVisible( true );
            }
        } );
    }

}
oliholz
  • 7,447
  • 2
  • 43
  • 82