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));
Related
Which is the best way to make a window that has a name a JLabel and two JButton components underneath.
I am new to Swing and I tried some methods but didn't understood too much from any.
What would you suggest to focus my attention on to do this specific thing?
DYM like this?
Or this?
Actually that 2nd one comes from a page linked in the first comment.
But, you might be over complicating the issue and a JOptionPane might be a more suitable solution - see How to make dialogs for more details.
I'd prefer it from Property editor -> Customize Code.
I need to select a folder in one instance and a file in another.
The function will be: On pressing jButton1, jFileChooser1 opens and on confirmation of selection, returns the folder/file path selected to a String strFolderPath/strFilePath.
Thanks in advance for any help.
"I'd prefer it from Property editor -> Customize Code."
No you don't. That's editing the auo-generated code. Yo don't want/need that. The auto-generated code is just for initializing the component and laying them out. It's not meant to be altered (unless you really know what you're doing)
What you want is to add a listener to the button, and write your code for choosing a file in the listener callback. In Netbeans editor you can simply:
Right click on the button from the design view and select Events->Action->actionPerformed
If you go to the source code view, you will see something like
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
Inside that method is where out write your code for the file chooser. If you need help with that, I suuggest you take a look at How to use File Choosers. A very simple code example can also be found at the JFileChooser api javadoc
As an aside, these are pretty basic Swing use cases. I would strongly urge you to put down the GUI editor and learn to hand code first. It will make using the editor as tool much easier once you understand the code behind it. If you do want to follow this advice, keep handy the Swing tutorials and slowly go through it at your own pace. There's a lot to take in, but no on can become a ninja overnight, you need to be a grasshopper first :-)
I want to give a user a convenient way to enter range from swing gui.
What can I use? A perfect option would be a slider with two pointers.
SwingX has JXMultiThumbSlider which is exactly what you're looking for.
Btw, this implementation of RangeSlider is more simple for me, but swingx stuff should work somehow too, if your implement Renderer first.
http://ernienotes.wordpress.com/2010/12/27/creating-a-java-swing-range-slider/
This is for an application so I don't want a hyperlink. I first tried using a Jbutton without all of border/background stuff and then hooking up an actionListener to it but I couldn't get it to the point where I thought it looked nice. I also tried using a JLabel and hooking up a mouse listener to that but I also couldn't get it to look right.
Basically I would like a way using swing to make a button exactly like a url link in an application. What is the standard way of doing this?
but I couldn't get it to the point where I thought it looked nice
You might want to go into greater detail on just what "looked nice" means. I can see you solving this by either a JButton or a JLabel, but the key is perhaps not to look for another solution but to play with the settings of the button or the label til they look nice. If you can't find a nice solution, then post your code (an SSCCE would work best of all) and perhaps we can help you.
that isn't answer to your question but are you tried to add ButtonModel to your JButton example here
It is a rather heavy hammer to use, but SwingX has a JXHyperLink control that is probably exactly what you want. The source is at http://java.net/projects/swingx/sources/svn/content/trunk/swingx-core/src/main/java/org/jdesktop/swingx/JXHyperlink.java?rev=4027 and you can see an article about it at http://www.javalobby.org/java/forums/t18617.html.
It is old, but SwingX continues to do good things.
It's you're trying to make a desktop application which looks like HTML inside a browser, you might try using some of the richer Swing text components in a read-only mode. You could use a mouse-listener to map X/Y clicks to a particular character of text, and then cause an action to occur on that basis.
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