Article View: borland.public.jbuilder.java.swing
Article #11506Re: Control Keys Alt+N
From: Jason Burton
Date: Thu, 13 May 1999 00:00
Date: Thu, 13 May 1999 00:00
38 lines
1987 bytes
1987 bytes
Sohan Mendis <sohan@maxtrak.com> wrote in message
news:7hdtf1$scv13@forums.borland.com...
>
> Normally in VB , we can set text to appear on button like 'New' ( N
> underlined). where we set button property as &New in the designer .
>
> How can we make that happen in JButton.
The setMnemonic method accomplishes this, though in a slightly different
way. You do a setText("New") and then a setMnemonic('N'). I have always
liked the Delphi/VB method of setting the accelerator key, so I overrode the
setText property of the button class that I always use. Here is the code I
use, though keep in mind that it doesn't allow the && to display an
ampersand in the button text (could be modified easily to do that, though):
public void setText(String value)
{
String newValue;
int index = value.indexOf('&'); // find the ampersand
if (index > -1)
{
setMnemonic(value.charAt(index + 1)); // set the hot key
if (index > 0)
newValue = value.substring(0, index) + value.substring(index
+ 1); // get the text before and after the ampersand
else
newValue = value.substring(index + 1); // get the text
after the ampersand only
}
else
newValue = value; // no ampersand passed in - set the text to
the value passed in
super.setText(newValue);
}
HTH,
Jason
Technical Details
Message-ID:
<7hem4s$sv735@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7hdtf1$scv13@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,ce97bd7850db5e6e X-Google-Attributes: gid111d45,public From: "Jason Burton" <Jason.Burton@McLeodSoftware.com> Subject: Re: Control Keys Alt+N Date: 1999/05/13 Message-ID: <7hem4s$sv735@forums.borland.com> X-Deja-AN: 477421554 References: <7hdtf1$scv13@forums.borland.com> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 Organization: Another Netscape Collabra Server User X-MSMail-Priority: Normal Newsgroups: borland.public.jbuilder.java.swing