• Post Reply Bookmark Topic Watch Topic
  • New Topic

Short-Cut Keys?  RSS feed

 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to add shortcut combinations to buttons and menu buttons.
I looked but for the menu I don't have any shortCut property?(i am using javax.swing.*, and java.awt.*)
I searched earlier posts anc came up with this:
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#create
If you are using Swing, you also have the option of
myMenu.setMnemonic( 'F' ) ;
If you are using AWT, try setShortcut() from MenuItem.

I read cindy say something about using keylisteners, but then reading that it's easier to use
myMenu.setMnemonic( 'F' ) ;
http://java.sun.com/j2se/1.3/docs/api/java/awt/MenuShortcut.html
But it's all hard to piece together.
I'm using Jbuilder, in
Frame1 extends JFrame it
create private JMenu jMenuFile = new JMenu();
and
private JMenuItem jMenuFileExit = new JMenuItem();
Later on for component initialization, in
void jbInit() throws Exception
it creates:
jMenuFile.setText("File");
for the file menu, but
then what about actual buttons like Exit, it create more code for them becuase of the actioan listener(event).
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jMenuFileExit_actionPerformed(e);
}
});
So, where do I put code like:
//Setting the accelerator:
jMenuFileExit.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_T, ActionEvent.ALT_MASK));
Or
myMenu.setMnemonic( 'F' ) ;
???
I hope I described my confusion well enough, I know about the code, use mnemonic and setshortcut, my problem is how to implemement it into my code?
thanks.
sorry that I'm asking a question already asked countless times.
[ November 16, 2002: Message edited by: Drake Silver ]
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, ok...sorry.
I didn't read ahead enough...or just enough.
So far I've read that you place the menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
code into the initialization of the menu.
I'm going to try that, see if it works.
If not I'll be posting again.
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent!
It worked.
I simply put this code into the jmenu initialization.
jMenuFile.setMnemonic(KeyEvent.VK_A);
ex:
jMenuFile.setText("File");
jMenuFile.setMnemonic(KeyEvent.VK_F);
Now, can anyone tell me how I could set shortcut keys onto buttons.
Say I want to allow for a user to simply type Alt+O for a button on a panel, that loads a text file.
I will also have this option in the Jmenu.
I found this:
equals(Object obj)
and
equals(MenuShortcut s)
Which would I use, would I use object becuase I'm comparing to a button object on a panel?
So that would solve the two of the same shortcuts problem that I might run into.
But now what about actually coding the shortcut key into the button, and the comparing using equals (Object button). The explanation is very vague on the website.
thanks
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright!
Same thing!
Just have to add the
jButton1.setMnemonic(KeyEvent.VK_O);
into the initialized section, again.
Wow.
I'm getting somewhere.
I guess that's it.
sorry for starting a whole new thread, turns out I didn't really need it.
Maybe I did, I just had to find myself writing(typing) and repeating this stuff to myslef for it to finally sink in.
Anyway, I hope this helps someone else out.
oh..actually.
The shortcut for the button does work, but I have to have the ALT key pressed, which kind of defeats the purpose, to a small extent.
Is there a way to make it so that if i'm focused in on the current panel, then using the Mnemonic
KeyEvent, could it trigger the buttons action.
As well, I have to ask.
In delphi, if you supply a file menu with an open file button, then also supply a button with an open file, you can just create one OnClick event, say on the button, then you could link the filemenu open file to the button's onclick event.
effectively making one proecedure but using it with two different buttons. This is easily done from the components property's toolbar.
Can you do that in java(jbuilder).
It woudl greatly reduce the amount of code I'll ave if I can make one function or method that would be used by both file menu and button when an onclick event was processed.

Edited:
Am I to understand that using the above code:
jBtnOpen.setMnemonic(KeyEvent.VK_O);
to use keys to activate buttons and menus can ne altered to something like this:
jBtnOpen.setMnemonic(KeyEvent.VK_Shift_O);
therefore allowing for both shift and O to be used simultaneously...
---2 mins later...
Nope, won't work.
what aboout
VK_PAGEUP and pagedown, if I added those to TextArea's Mnemonic keyevents, would that let me use pageup and page down when in the textarea itself.
That would be pretty cool, advanced even..for me.
[ November 16, 2002: Message edited by: Drake Silver ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!