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
Related
I can make control extending Composite or Canvas.
How to make it selectable? I.e. how to make it behave like Button? Button is disabled for extend. I see a lot of unportable code inside it.
So how to make Button-like control of myself?
Should I process mouse and keyboard events myself or there is some premade functionality to utilize?
You will have to handle the events yourself. I'll give you a couple of hints and references here:
First of all, make sure you read this: Creating Your Own Widgets using SWT
Have a look at SquareButton. It's a custom Button widget and should contain all the code you need.
Here is a very related SO question.
Hope this helps.
Canvas is for drawing from "inside". So, you have to create your own UI input. As here.
I have a Java Swing app in Which I have been able to set shortcut keys using the following piece of code. For example Ctrl+K.
keyHelp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, Event.CTRL_MASK));
keyHelp.setMnemonic((int) 'K');//This is the Line I need Help in
I just can't figure out how to add the same using F1 key as the shortcut... Could anyone please help?
Using Action, as shown here and here, can make these settings easier to manage. Also consider getMenuShortcutKeyMask() instead of assuming Event.CTRL_MASK.
use,
KeyEvent.VK_F1
I think it will help you.
If you need to make your shortcut key just "F1" without CTRL, It should be like this,
keyHelp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
A Javadoc from MMenuElement says:
String org.eclipse.e4.ui.model.application.ui.menu.MMenuElement.getMnemonics()
Returns the value of the 'Mnemonics' attribute.
If the meaning of the 'Mnemonics' attribute isn't clear, there really should be more of a description here...
Returns:
the value of the 'Mnemonics' attribute.
I strongly suspect, that Mnemonics attribute has something to do with hot-key shortcuts, like in Swing or AWT. But still, it is not quite clear, how to use them on Eclipse4 platform correctly. For example, how to assign Alt+F to a "File" menu item?
Can anyone provide me with a clue, example or HOWTO on this topic?
This is only the default javadoc as set by the EMF generator. So there was no real documentation done up to now.
You're right, mnemnoics got to do with shortcuts, but not the way you describe it. You are talking about key bindings, mnemonics are this:
&Open leads to a menu text with an underlined O which indicates keyboard accessibility. This shows to the user that the command is reachable using the platform specific accelerator. It is however platform dependent on how you see them, on OS X for example the accelerator is shown next to the label and hence has no effect.
see the javadoc of org.eclipse.swt.widgets.MenuItem.setText(String string) for a detailed explanation. The e4 model simply results in this call on the element, which happens in org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem:499
OS X information Mnemonics are not shown on Mac by definition, see Java Development Guide for OS X for the design definition.
I have a problem which I am currently trying to wrap my head around, and any advice or nods in a good direction would be greatly appreciated.
I want to display a Google Map within my Java Swing project, (the map will be a URL specified within an HTML document I think).
I also want to be able to communicate and interact with the map using JavaScript, injected via buttons in java swing, etc. So for example, I could have java buttons 'Satellite', 'Hybrid', and 'Earth' next to the map, and clicking them would perform the corresponding javascript action on the map. JavaScript methods would probably already be created within the HTML file (such as 'switchToSatelliteMap'), it would just be a matter of calling them within Java.
Thanks in advance for any help whatsoever,
tre.
I don't know whether this answers your question at all, but I think you will find these links helpful:
http://today.java.net/article/2007/10/24/building-maps-your-swing-application-jxmapviewer
http://swinglabs.org/downloads.jsp
GoogleEarth inside Java Swing
Is there a Swing component for Google Maps?
http://code.google.com/p/gameplan/source/browse/trunk/src/org/crazydays/gameplan/map/swing/JMapFrame.java?spec=svn62&r=62
I am more of a SWT fan, so I would have used a browser control as it allows me to execute javascript on the browser component. But again its a design choice.
Hope this will help.
I am not entirely clear on the scope of your issue, but you may find it helpful to attach MouseListener interfaces to your swing buttons
http://download.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html
http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
Implementing the mouseClicked method to run the required java script.
Additionally you could use a MouseAdapter if all you need is the mouseClicked functionality.
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