This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Underline first character of a JButton? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Underline first character of a JButton?" Watch "Underline first character of a JButton?" New topic
Author

Underline first character of a JButton?

Adam Till
Ranch Hand

Joined: Oct 11, 2002
Posts: 41
Hi
I have set the mnemonic on a JButton but how do I underline the specified character?
Thanks for your help.


Adam Till<br />SCJP SCJD SCWCD
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I'm using JDK1.4.1_01 and it looks like setMnemotic( int ) automatically underlines the first occurance of the mnemotic character in the title of the button... You are using the appropriate KeyEvent.VK_* for the parameters, right? So I'm not sure what you're having problems with... Could you provide an example of the code that doesn't work?


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Adam Till
Ranch Hand

Joined: Oct 11, 2002
Posts: 41
Hi
I have managed to get it working with the following:-
String localLabel = "<html><a style='text-decoration:underline'>L</a>ocal Connection</html>";
JButton local = new JButton(localLabel);
It did not seem to add the underline automatically, I am using the same JDK. So I used HTML, several HTML tags are being deprecated so I had to use CSS inline. Is this the only way I wonder?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Hmmmm... it's working for me to do :



What OS are you using, and what look and feel?
Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
If you have a second button which starts with "L" and you want to use to underline "L" would not work for the second button, HTML is the way to go.
Thank you
Jawad
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Ummm... sorry, Jawad... but it's working for me in that case, too...

The mnemotic only actually works for the last component it was applied to, but the appropriate character on all the components it was applied to are underlined...

Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
Thank you, for correcting me.
Jawad
Adam Till
Ranch Hand

Joined: Oct 11, 2002
Posts: 41
Right, figured the problem. My platform is XP and when my app starts I set the look and feel to getSystemLookAndFeelClassName(). If I remove this then yes it works.
I am still getting used to the 'look and feel' aspects, but what I was trying to do was use any systems default (for no other reason than scientific purposes so could just use the java defualt).
Any comments?
Thanks all,
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

That's weird... the Windows Look & Feel doesn't underline at all... Guess it's just yet another way Windows is broken...
 
 
subject: Underline first character of a JButton?