The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

RE: Controlling the Layout of a disabled

Posted by Kishori Sharan on July 26, 2000 at 5:15 PM

Hi
I got another solution and it seems very easy. If you want to change the color of a JCombobox you have to write just one line of code.
UIManager.put ( "ComboBox.disabledForeground", Color.blue ) ;
This will make the color of all JComboBox in disabled state as blue. However, this doesn't work for a checkbox. For a checkbox when you change the property to make it disabled, its getBackGround ( ) method is called. getBackground ( )
method returns a Color object. Then darker ( ) method is called on that Color object to set the final disabled color. So you need to override the getBackground ( ) method of the JCheckBox to achieve this. I have done it in the follong example.

Thanx
Kishori
/////////////////////////////////////////////////////////////////////////////

import java.util.* ;
import javax.swing.*;
import java.awt.*;
import java.awt.event.* ;

/* This class is defined to be used in getBackground() method of MyJCheckBox
class. You may not need this class if you preffer defining inner class in that
method itself*/

class MyColor extends Color {
MyColor ( int r, int g, int b ) {
super ( r, g, b ) ;
}

public Color darker ( ) {
return Color.red ;
}

}


class MyJCheckBox extends JCheckBox {
public MyJCheckBox ( String s ) {
super ( s ) ;
}

/* When we disable the checkbox then the getBackground ( ) method on it is
called which returns a Color object. Then the darker ( ) method is called
on that Color method and the returned Color is used as the disabled color
for that checkbox */

public Color getBackground() {
if ( isEnabled() )
return super.getBackground();


Color base = super.getBackground();
return new MyColor(base.getRed(), base.getGreen(), base.getBlue()) ;

/* Uncomment the following and comment the above line, if you don't want
to define a new MyColor class

return new Color(base.getRed(), base.getGreen(), base.getBlue()){
public Color darker(){
return Color.blue;
}
}; */

}

}


public class H1 extends JApplet {

String[] str = { "Hello", "Thanx", "Bye" } ;

MyJCheckBox jcbx1 = new MyJCheckBox ( "Enabled" ) ;
MyJCheckBox jcbx2 = new MyJCheckBox ( "Disabled" ) ;
JComboBox jcmb1 = new JComboBox ( str ) ;

public void init ( ) {

Container cp = getContentPane () ;


cp.setLayout( new FlowLayout ( ) ) ;

/* Set the color for disabled combo box in UIManager. However, we cannot
do the same thing for a checkbox because UIManager is not used
to set the foreground color of a checkbox rather the getBackground
method is called and the darker of the color returned from getBackground
method is used */

UIManager.put ( "ComboBox.disabledForeground", Color.blue ) ;



jcbx2.setEnabled ( false ) ;
jcmb1.setEnabled ( false ) ;

cp.add ( jcbx1 ) ;
cp.add ( jcbx2 ) ;
cp.add ( jcmb1 ) ;


}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us