Handling JavaFX event in function key - java

How can I add a function key (i.e. the F1 to F12 keys) for shortcut key in JavaFX?
I use save button. I don’t need to click save button and it make easy to system

If you are using a Button, let's say saveButton and it is in Scene scene then you can set accelerator(shortcut key) to button as following:
Button saveButton = new Button("save");
scene.getAccelerators().put(new KeyCodeCombination(KeyCode.F1), saveButton::fire);
KeyCodeCombination in above code is used to set accelerators to javaFX contols and It takes, as argument, KeyCode e.g. KeyCode.K, KeyCode.F3 etc. and/or KeyCombination like KeyCombination.SHORTCUT_DOWN etc.
and if you are using MenuItem let's say saveMenu then you can set accelerator(shortcut key) to it as following:
MenuItem saveMenu = new MenuItem("save");
saveMenu.setAccelerator(new KeyCodeCombination(KeyCode.F1));

Related

Add SWT context menu in SWT Table with Shortcut Keys

I am trying to add a context menu on SWT Table with the Key Name.
Context Menu is coming properly but I am not able to set the key name as we can mention as a "sequence" in Menu Contribution.
I am not using Menu Contribution but using a MenuItem.
Here is my code.
final MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText(save);
item.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
//saveFunction
}
});
This is working but I want to add the Key name also with the name of Menu something like this:
Can anyone please help me as I can not use MenuContribution.
You can use MenuItem.setAccelerator to set the key for the menu item:
item.setAccelerator(SWT.MOD1 | 'S');
Note that this is only active when the menu is shown, if the menu is not shown you would need to use a key listener. I have used the SWT.MOD1 modifier here rather than SWT.CTRL so that the key will be the correct ⌘+S on macOS.
On platforms that don't automatically add the accelerator text you can get set the text using:
String acceleratorText = Action.convertAccelerator(SWT.MOD1 | 'S');
item.setText(save + '\t' + acceleratorText);
Here Action is org.eclipse.jface.action.Action.
In an Eclipse plug-in you should probably be using a retargetable action to integrate with the standard save code.

Allow user to redefine hotkeys for Swing during runtime Java

I have a Java application that contains a lot of features, and to make life easier for the user, I have set up numerous mnemonics and accelerators. For instance, I have a JMenuItem that allows the user to save the state of the application, witht he following code:
JMenuItem saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
This works as desired, but now I would like to give the user an option to change the hot keys. While CTRL + s would seem like a fairly obvious hot key to stick with, there are many features that use these short cuts, and simply picked Save as an example.
I have a JButton that I have arranged for testing purposes that allows the user to enter in a new shortcut when clicked. I was thinking that I would simply try and capture the keys that the user holds down (InputEvent) and presses (KeyEvent). I also though it might be smart to force the use of an InputMask to avoid complications in Text Fields and the like.
What I am wondering is: What is the best way to capture the new input that the user enters? I have looked up information regarding KeyBindings and they look right for the job, but the main issue I see is actually capturing the keys and saving them.
Sounds like you need to setup a KeyListener. When the user presses/releases a key, it triggers a KeyEvent from which you can retrieve the main key pressed (e.g. S) and the mask/modifiers (e.g. CTRL+SHIFT).
From there you can create a KeyStroke object and set this as the new accelerator of your menu.
public void keyReleased(KeyEvent e){
KeyStroke ks = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers());
menuItem.setAccelerator(ks);
}
The thing is you probably want this key listener to be removed right after the key released event, to avoid multiple keystrokes to be captured. So you could have this kind of logic:
JButton captureKeyButton = new JButton("Capture key");
JLabel captureText = new JLabel("");
KeyListener keyListener = new KeyAdapter(){
public void keyReleased(KeyEvent e){
KeyStroke ks = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers());
menuItem.setAccelerator(ks);
captureText.setText("Key captured: "+ks.toString());
captureKeyButton.removeKeyListener(this);
}
};
ActionListener buttonClicked = new ActionListener(){
public void actionPerformed(ActionEvent e){
captureKeyButton.addKeyListener(keyListener);
captureText.setText("Please type a menu shortcut");
}
};
captureKeyButton.addActionListener(buttonClicked);

How to create controls dynamically in JFace Wizard

I have a jFace wizard, I am using this to create a new project type eclipse plugin. As you can see from image below, I have one treeviewer on left side, and a SWT group on right side. What I want is when ever user selects one of the item from treeviewer, I should be able to create dynamic controls on right side SWT Group. Say user selects Test One, one right side I should be able to create few controls like label, text and few radio buttons on right side, similarly if user selects Test Two I should be able to create dynamic controls on right side.
Currently I tried below code:
tree.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
for (int i = 0; i < selection.length; i++) {
String tempStr = selection[i].toString();
tempStr = tempStr.replaceAll("TreeItem \\{", "");
String finalStr = tempStr.replaceAll("\\}", "");
if (finalStr.equals("Test One")) {
Button btn = new Button(g2, SWT.NONE); //g2 is right side group
btn.setText("Blaaaa");
btn.setVisible(true);
container.redraw();
}
}
But when I run, I see no changes on right group. Can anyone guide me what I am doing wrong? Any pointers would be very appreciated, since I am new to Eclipse development and SWT.
You probably didn't set a layout on the g2 group. This is the common cause for controls not showing up. You can also try using g2.layout() to ensure that the new controls are correctly laid out after you create them.
Additionally you could look at using a StackLayout so that once you create a set of controls you can just hide them all at once instead of destroying when the selection changes. This is often useful so that if the user comes back to a previous selection, they will find the data they entered in the same state when they switched the selection. Here is an example.

How to catch Enter key and change event to Tab in Java

I have a swing application with multiple jtextfield on it. How do you replace the function of the enter key wherein when you press the Enter key, it will transfer to the nextfocusable component just like the tab key? I dont want to put a keylistener on each jtextfield.
You're looking for Container.setFocusTraversalKeys:
Container root = ...
// pressed TAB, control pressed TAB
Set<AWTKeyStroke> defaultKeys = root.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
// since defaultKeys is unmodifiable
Set<AWTKeyStroke> newKeys = new HashSet<>(defaultKeys);
newKeys.add(KeyStroke.getKeyStroke("pressed ENTER"));
root.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newKeys);
For more information, take a look at the Focus Subsystem tutorial.
You can call:
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.focusNextComponent();
but you will have to register a single ActionListener with all your JTextFields.

Java Swing Key Press Event

I am using my predefined inherited Focus Traversal Class For My JFrame
I have defined the key press event for one of my button with some action on pressing Tab key to select other tab of my jTabbed Pane . This button is not responding only for the tab key .
int index=1;
if(evt.getKeyCode() == KeyEvent.VK_TAB)
{
// wrap around
if(evt.isShiftDown())
{
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent();
}
else
{
System.out.print("Shift Up");
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
jtabPaneProducts.setSelectedIndex(index);
}
}
Please guide me how can i made jbutton to respond to the TAB key press in addition to focus traversal functionality.
You should be interested to read How to Write a Key Listener:
Alternatively, you can use the KeyEventDispatcher class to pre-listen to all key events. The focus page has detailed information on the focus subsystem.
And consequently: Interface KeyEventDispatcher

Categories

Resources