Creating Button Shortcuts with Java like & (ampersand) with Windows Forms - java

I wish I could word this question better. Partly I just don't know the name of what I'm looking for.
With Visual Studio and Windows forms you can add an ampersand to create a "shortcut" key to a button by pressing Alt + [That Letter]. Like My &Button ... Alt + B would be the shortcut.
Is there an equivalent of this in Java SWING? I am using NetBeans IDE 6.9.1.
Thank you all!

That's called a mnemonic.
Use setMnemonic (javadoc)
See http://download.oracle.com/javase/tutorial/uiswing/components/menu.html#mnemonic

You use JMenutItem.setMnemonic(); I don't think there's a handy-dandy shortcut like the ampersand.

Related

Underline the first character of a MenuItem?

Please help me with this matter. Is there any way to underline the first character
of this MenuItem in Java? This is a WindowApplication writing by Eclipse.
Assuming this is an eclipse RCP application, or at least a GUI based on SWT (as opposed to swing or JavaFX).
Mnemonics is the term.
menuitem.setText("Zoom &In");
to underline "I."
Besides you probably want accelerator: Ctrl +/-/0 are customary for zoom in/out/reset.
menuitem.setAccelerator(SWT.CONTROL|'+');
Sometimes the underlining is only visible in some mode.

How can i find where a method or a variable is used in Android Studio (shortcut)

I know how to find the source by using ctrl + left click on a method, for example, but what if I want to find where this method is used? I'm using Windows 7.
Let's suppose I have the following method:
class A {
public int sum(int a, int b) {
return a+b;
}
}
and I am using this method in
class B {
...
a.sum(c, d);
...
}
and I want to find where I used sum while I was in class A.
PS: I'm new to Android studio (started using it about a month ago).
In Android Studio, highlight the method and either right click > Find usages or use the Alt+F7 shortcut.
Edit: Ctrl+left click on a method is useful for finding where that method was initially declared
For Ubuntu users:
For me Alt+F7 has not worked,
so I've solved the conflict by going to the
Ubuntu System settings-> keyboard -> shortcuts
and changing Alt+F7 in there with another combination.
And now it's working ))
Just click on the method or variable and any usages will be highlighted and on the scroll bar on the right side of your code you will see gray bars appear, those are the usages of the current highlighted (or clicked on) variable or method within your code.
Here are different key shortcuts for displaying method or variable usage. The keymaps with F14 in them were added by me; the others are default. For Windows (and Linux?), replace the ⌘ key with Ctrl (I think). Find Usages shows them in a dedicated window in Android Studio (the solution in other answers), while Show Usages puts them in a popup (second image below).
To find all uses of a variable or method in Android Studio you can use
ctrl+shift+F7(window/Ubuntu)
after that for navigation purpose you can use F3
use [Ctrl+b] or [Ctrl+Alt+b] to navigate to the origin of the method or variable declearation and implementation
Ubuntu is Ctrl+Alt+7 or Ctrl+b to show the usage of the class. Ctrl+Alt+F7 is not work.

Can't find Eclipse shortcut for Renaming

Used to know this, think its something like Ctrl + ??? + F11. Or something.
I have a class member/property that I would like to change the name of. Unfortunately it is sprinkled all over the class, some 100+ references to it. Throughout the various methods.
Eclipse has a shortcut that allows you to click on an identifier, hit the hotkey (whatever it is), and then modify the text composing the identifier. Then, once you click off of that text, it searches and replaces all references of the "old" identifier and replaces them with the new one.
I know I could just do a search & replace. I don't want that. I want the shortcut/hotkey. And its driving me crazy because I can't find it because I don't know what it's called!
Thanks to any Eclipse gurus that can help.
The shortcut combination is ALT + SHIFT + R.
CTRL+SHIFT+L list of all shortcuts.
The key combination is
Alt + Shift + R
The corresponding menu (and context menu) entry is
Refactor > Rename ...
I wanted to specify that the shortcut is Alt + Command + R if you're using Mac OS X.
PC
The shortcut for rename is:
Alt + Shift + r.
And the list of shortcuts will be displayed with:
Ctrl+Shift+L
Mac OS x
The shortcut defined for rename/refactoring will work:
option + ⌘ + R
The option for rename is:
F2
Both are shortcuts defined for Mac, open the Shortcuts List with
Shift + ⌘ + L:
Simply select the file and press F2. The rename option will open. Other option is ALt + Shift + R. But I prefer F2 is much easier way.
For renaming a file, the F2 works in all cases I've encountered.
However my experience shows that refactoring a class or a method can behave differently in different OS and desktop environments and that they may intercept key modifiers and thus render shortcuts ineffective.
However, eclipse is good at accessing its main menu from the keyboard. For instance, in plasma in linux (tested with eclipse 2019-09 and older) renaming is achieved pressing Alt-T, then releasing it and pressing N afterwards. The Alt-T effectively opens up the refactoring menu and the N orders eclipse to rename the selected element. Actually, the whole refactoring menu can be addressed in that manner, this achieving the goal with less friction.
For Mac, use the below keys
OPTION + COMMAND + R

Short cut keys in java

How can I write programs for shortcut keys in java. For Example, ctrl+s means the save option should work. Can anyone help?
Basically you're need to set mnemonics for appropriate buttons. Also you can set some accelerators for menu items. Here is a rather good example.
You want to read the Java tutorial for
Key Listeners
Swing was designed to use Key Bindings for this type of functionality. You create an Action and map a KeyStroke to invoke this Action. Read the section from the Swing tutorial on How to Write an Action for more information.
If you're developing on Mac OS X, use accelerators instead of mnenomics, which are not suppported under the native look and feel Apple Java development guide

Inject a keystroke in java

I'm looking for a way to inject a keystroke into the OS keyboard input buffer,
like when you click a button the program inserts one (or more) keyboard strokes. I wanted to do this in java because I want to run this in (win,linux and osx). I guess that I'll have to make use of the JNI, do anyone have some ideas?
Thanks all stackoverflowers ;)
My guess is that the java.awt.Robot class will do this for you:
new Robot().keyPress(...);
http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#keyPress(int)
java.awt.Robot "is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed."
Check java Robot . I believe this is what you are looking for.
Also check this out. Example

Categories