I tried the usual way to handle keypress: implements KeyListener and override keyPressed(). But there are many swing components, and if Im on a combobox, the combobox "thinks" the key is for him. I want to the main app capture the keypress, how to?
You are looking for this Key Bindings
You could use:
KeyEventPostProcessor kepp = new KeyEventPostProcessor() {
#Override
boolean postProcessKeyEvent(KeyEvent e) {
// handle key event globally
}
};
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
kfm.addKeyEventPostProcessor(kepp);
This is way harder than you'd think. For example, here's the effort you have to put in to get this working on a Panel with a JFileChooser in it. That's a similar question I asked in the past. I think that will help you solve your problem. You should read both answers given as they're both valid.
To summarize, you've gotta iterate all the InputMaps and the parents of the InputMaps and clear out the keys you want to globally use. Then apply your action to the KeyMap.
You'll need this:
private static void clearInputMap(InputMap inputMap) {
inputMap.clear();
while ((inputMap = inputMap.getParent()) != null) {
inputMap.clear();
}
}
Related
When I am editing grid inline I can save or cancel my grid row changes. I want to update my database entries after button 'save' will be pushed(Data base mechanism has already done) How can I implement it?
My container:
BeanItemContainer<CategoryOfService> beansContainer;
Editing view:
All what I need it know which listeners I have to use. I found some CommitHandler which I can add by EditorFieldGroup class but I can't implement it properly maybe there is have to be another way to resolve problem.
There's kind of a way to capture inline Save click on the grid.
grid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler() {
#Override
public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
//...
}
#Override
public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
//...
}
});
After clicking Save both methods preCommit and postCommit get called.
Hope it helps :)
Grid does not currently give you any direct way of adding listeners to the save and cancel buttons for the inline editor, although that is something that might change in Vaadin 7.6.
As a workaround before that happens, the CommitHandler approach that you already mentioned is still supposed to work. You can find a basic example here. The contents of your BeanItemContainer should be fully updated in the postCommit phase.
grid.getEditor().addSaveListener((EditorSaveListener<Product>) event -> {
//your stuf
HibernateDataSource.updateProduct(event.getBean());
});
I am developing an IDE. I created a CustomTab.java class by extending the default JavaFX Tab. I wanted to add a color transition animation, but I ran into a problem. The method in TabPane named getTabs(), which returns ObservableList<Tab>, is final. This means I can't override it to return ObservableList<CustomTab>. It seems that constructions like this:
for (Tab tab : tabPane.getTabs()) {
((CustomTab) tab).stopFlash();
}
used in Controller.java are neck breaking and wrong.
What is the right way to do this?
I think my reply is quiete late, but I think there is a way to do so.
Use the function
tab.setUserData(Object c);
In that Object you can store all the informations you'll need to proceed.
so for example create an Object holding all the objects you need.
E.g. you can hold the reference to your webView.
Also if you need to have different informations you could use:
tab.getProperties().put(Object key, Object data);
// receive the value with
tab.getProperties().get(key);
Here a link to the documentation I found
Maybe the Javadoc is better, but I found this one.
Here is a little example that I wrote:
public SQLEditorTab getNewTab(){
SQLEditorTab tab = new SQLEditorTab("new Tab " + number);
tab.setId("newTab" + number);
tab.setOnCloseRequest(this);
tab.setUserData(tab.tabContent);
number ++;
return tab;
} // just to create the tab
In my listener I receive the result simply by:
sqlEditorPane.getTabs().forEach((Tab t) -> {
if(t.getUserData() != null){
System.out.print(t.getUserData().getClass());
}
});
I want to get my program to unhide main window when user presses some shortcut. Is there a way to get the global key events, not only the ones which happened when focus was inside application frame?
This might do what you want. Note that this code is checking for a Ctr-F keystroke. I use this code to open up a find dialog from anything in the application. I'm pretty sure that the app has to have focus though. Something to try at least...
AWTEventListener listener = new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
try {
KeyEvent evt = (KeyEvent)event;
if(evt.getID() == KeyEvent.KEY_PRESSED && evt.getModifiers() == KeyEvent.CTRL_MASK && evt.getKeyCode() == KeyEvent.VK_F) {
}
}
catch(Exception e) {
e.printStackTrace();
}
}
};
Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.KEY_EVENT_MASK);
EDIT: I think I understand what you want. Basically when the app does NOT have focus. If so then you'll probably have to hook into the OS events with a native API (JNI) but that forces you to a specific OS...
This might be useful. I'm not sure if there is one library that will work for Windows/Linux/Mac. For Windows you will need some external library that uses native code to create a keyboard hook. I have no idea how to do it on the other OSes.
A solution to do this by using a JFrame is to set his opacity to 0.0 and to add the Keylistener to it. But the user will see an icon in his shortcut bar...
I need some help. Is it possible to simulate a drag & drop without registering a component?
E.g. I click the mousekey anywhere on the window and hold the mousekey down, at this moment, I want to create or simulate a DragSourceEvent programmatically with Java.
Is this possible?
Update:
Regarding Bob's reply, at least I got it, I can create a listener for the drag & drop:
DragSource dragSource = new DragSource();
DragGestureListener listener = new DragGestureListener() {
public void dragGestureRecognized(DragGestureEvent event) {
event.startDrag (null, strSel) ;
...
}
}
listener.dragGestureRecognized(new DragGestureEvent(
new DragGestureRecognizer(dragSource, component) {
}, DnDConstants.ACTION_COPY, new Point(0,0), events ));
but unfortunately i get this exception:
java.lang.IllegalArgumentException:
source actions at
java.awt.dnd.DragSourceContext.(DragSourceContext.java:169)
at
java.awt.dnd.DragSource.createDragSourceContext(DragSource.java:454)
at
java.awt.dnd.DragSource.startDrag(DragSource.java:293)
at
java.awt.dnd.DragSource.startDrag(DragSource.java:403)
at
java.awt.dnd.DragGestureEvent.startDrag(DragGestureEvent.java:203)
any suggestions?
The question you asked:
I haven't tried it, but in theory you should be able to create the Event object and get a handle on the Swing Event Queue from one of the system classes. However without having a valid component, there may be problems when methods try to work with the event.
What you probably meant:
Registering events for a standard window -- you should be able to set up drag and drop support for an empty JPanel or JFrame, but it'll take some hacking. Drag & Drop is a pain to work with at this level when not built in -- I suggest using something like an invisible component or something instead.
I apologize if this question is too basic, but I just can't figure out how to do this. I have a SWT TableFolder with two tabs, and I need to determine which of those two tabs are currently active, as it effects the behavior of another part of the program. Is this possible? Thank you in advance.
To you mean the org.eclipse.swt.widgets.TabFolder (CTabFolder)?
If yes add an eventlistener to your TabFolder(CTabFolder object
tabFolder.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent event) {
tabFolder.getSelection()[0]; // This should be your TabItem/CTabItem
}
});
If you simply have a javax.swing.JTabbedPane then calling
yourJTabbedPaneVariableName.getSelectedIndex()
gives you the index of the selected Tab