aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes setMnemonic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "setMnemonic" Watch "setMnemonic" New topic
Author

setMnemonic

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I want to control my buttons in keys instead of mouse.for that I have used setMnemonic.but now too its not working.here is my code.pls correct the code and help me.

public class AstMonitor extends JApplet implements
ActionListener, Runnable {
Thread astThread;

JButton logout = new JButton("logout");
public void init() {

GridLayout gl;
getContentPane().setLayout(new GridLayout(3,10));
logout.setActionCommand("logout");
logout.addActionListener(this);
logout.setMnemonic('l');
this.getContentPane().add(logout);
logout.setBackground(Color.pink);
astThread = new Thread(this);
astThread.start();

}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
String thebase = "http://" + getCodeBase().getHost();
if (command.equals("logout")) {
getAppletContext().showDocument(String2Url(thebase + "/crm/crmlogin.html"),"_parent");
actionSocket("Logoff",true,getParameter("thehost"),getParameter("station"),getParameter("manageruser"),getParameter("managerpass"));
stop();
}

anyone help me pls.

Thanks..
rachana rayaguda
Greenhorn

Joined: Aug 05, 2007
Posts: 16
Originally posted by preethi Ayyappan:


logout.setMnemonic('l');



try this instead:

logout.setMnemonic(KeyEvent.VK_l);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

I think that should be an L. When using mnemonics, it's best to always use uppercase characters, or the KeyEvent.VK_ constants.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
rachana rayaguda
Greenhorn

Joined: Aug 05, 2007
Posts: 16
Originally posted by Rob Prime:
I think that should be an L. When using mnemonics, it's best to always use uppercase characters, or the KeyEvent.VK_ constants.


umm...yeahh..KeyEvent.VK_L
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks.I put VK_L instead of l.but thats also not working.whether i have to implement keylistener?if i implement that with actionlistener, its displaying an error messages as follows...


Class must implement the inherited abstract method KeyListener.keyReleased(KeyEvent)

Class must implement the inherited abstract method KeyListener.keyPressed(KeyEvent)

Class must implement the inherited abstract method KeyListener.keyTyped(KeyEvent)

what should i do now?
rachana rayaguda
Greenhorn

Joined: Aug 05, 2007
Posts: 16
hii preethi,
The mnemonic can be activated by pressing 'Alt' and the mnemonic.
in that case...

also works with that.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks rachana.but my doubt is without implementing keylistener,will this code work?keylistener is not needed in this case?
rachana rayaguda
Greenhorn

Joined: Aug 05, 2007
Posts: 16
yeah..it will work..no need to implement keylistener interface.

i checked and both of these are working(when pressed alt+mnemonic)

logout.setMnemonic('l');(->Uppercase and lowercase letters are equivalent for mnemonics.)
logout.setMnemonic(KeyEvent.VK_L);(-> here it shud be VK_L not VK_l as i had suggested before)

Hope that helped.
[ October 17, 2007: Message edited by: rachana rayaguda ]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi rachana,

Thank you so much.Its working well.when pressing the mnemonic with alt key.I was trying simply by pressing the mnemonic key alone.thats y i didnt get the result.but for the first time i have to click a button with the mouse.after that only its working with keys .the button needs a focus .Is there any code to get the focus on the button by pressing keys without using mouse?
rachana rayaguda
Greenhorn

Joined: Aug 05, 2007
Posts: 16
Originally posted by preethi Ayyappan:
Hi rachana,

Thank you so much.Its working well.when pressing the mnemonic with alt key.I was trying simply by pressing the mnemonic key alone.thats y i didnt get the result.but for the first time i have to click a button with the mouse.after that only its working with keys .the button needs a focus .Is there any code to get the focus on the button by pressing keys without using mouse?


try this:


[ October 17, 2007: Message edited by: rachana rayaguda ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by rachana rayaguda:




[ October 17, 2007: Message edited by: rachana rayaguda ]


Since Swing painting(including focus painting) happens on the Swing thread, this wont quiet work.
You will need something like the following as the last line in your constructor.

This will ensure that when your App. window is displayed, the logout button has the focus.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
could you please tell me where should i place your code exactly?

code:

public class AstMonitor extends JApplet implements
ActionListener, Runnable {
Thread astThread;

JButton logout = new JButton("logout");
public void init() {

GridLayout gl;
getContentPane().setLayout(new GridLayout(3,10));
logout.setActionCommand("logout");
logout.addActionListener(this);
logout.setMnemonic('l');
this.getContentPane().add(logout);
logout.setBackground(Color.pink);

SwingUtilities.invokeLater(new Runnable(){
Override
public void run() {
logout.requestFocusInWindow();
}
});
astThread = new Thread(this);
astThread.start();

}.
I have placed your code like this.I am getting the following error.

"Syntax error on keyword "public"; "Identifier" expected
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setMnemonic