Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setMnemonic

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by preethi Ayyappan:


logout.setMnemonic('l');



try this instead:

logout.setMnemonic(KeyEvent.VK_l);
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that should be an L. When using mnemonics, it's best to always use uppercase characters, or the KeyEvent.VK_ constants.
 
rachana rayaguda
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii preethi,
The mnemonic can be activated by pressing 'Alt' and the mnemonic.
in that case...

also works with that.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks rachana.but my doubt is without implementing keylistener,will this code work?keylistener is not needed in this case?
 
rachana rayaguda
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic