Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2005

Tip: Looking for answers? Try searching our database.

How to grey-out jButton with HTML in label

Thread view: 
michael.o'connor@bluescopesteel.com - 28 Jun 2005 01:54 GMT
I have a jButton with HTML embedded in the label text, and when the
jButton is disabled, the label is not greyed-out in the usual way. How
do I get around this? Below is a code snippet illustrating the
behaviour:

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

public class Test{
 JFrame f = new JFrame();
 JButton jButton1 = new JButton(
"<html><p align=\"center\">Button with HTML<br>in the
caption</p></html>");
 JButton jButton2 = new JButton("Another button without any HTML");

 public Test(){}

 public void launch(){
   jButton1.setEnabled(false);
   jButton2.setEnabled(false);

   f.getContentPane().setLayout(new GridBagLayout());
   f.getContentPane().add(jButton1, new GridBagConstraints(
       0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
       GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
   f.getContentPane().add(jButton2, new GridBagConstraints(
       0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
       GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

   f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   f.pack();
   f.setVisible(true);
 }

 public static void main(String[] args){
   Test t = new Test();
   t.launch();
 }
}

Mike.
Filip Larsen - 30 Jun 2005 09:08 GMT
Mike wrote

> I have a jButton with HTML embedded in the label text, and when the
> jButton is disabled, the label is not greyed-out in the usual way. How
> do I get around this?

To my knowledge, the enabled state is not carried over to the ultimate
component responsible to render the html text (see the source code for
javax.swing.plaf.basic.BasicButtonUI and
javax.swing.plaf.basic.BasicHTML for details here).

You can simulate the effect of disabling by setting the foreground color
just like the BasicButtonUI do on non-HTML text:

jButton1.setEnabled(false);
jButton1.setForeground(jButton1.getBackground().darker());

and set it back to its previous color when you re-enable the button. If
you have many buttons like this you can write a small class that listens
on the button and do it all for you.

Regards,
Signature

Filip Larsen



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.