In my JavaFX application I need to detect when the user canceled a drag action (like right clicking while dragging or pressing ESC button while dragging). I tried to listen on every DragEvent type on my main window, like DRAG_EXITED_TARGET or DRAG_EXITED but those are fired even if the cursor moves to another Node... I'm unable to narrow it to the single event of canceling the drag action.
I read that event.getGestureTarget() == null could mean that the user canceled the action, but it's also null when it leaves to another Node.
I managed to get this working for me by adding a PropertyChangeListener to a window's focusedProperty. When the user cancels the drag action, my window is losing the focus so this works for me.
Related
I want my program to behave diffrently when the user presses a component, and when a user drags the mouse over a component, the problem is that on mouse click both of these methods are being called (and it seems like mouseDragged is called after mousePressed), so how will i know if the user dragged his mouse or just pressed it?
The correct answer is to use mouseClicked instead of combination of mousePressed+mouseReleased in case you want to distinguish between a click and a drag.
I have a JTable with a custom cell editor. The editor implements FocusListener so I can check if the cell's contents is valid if the user clicks away from the cell.
I'd like to use a JOptionPane within focusLost (in the EventDispatchThread) to allow the user to select whether to revert to an old value or accept an adjusted value.
Here's the problem; if the user is editing a cell, then clicks a button away from the table, the button's actionlisteners are alerted before JOptionPane has returned.
This is what I'd like to happen:
User edits cell
User clicks button
Cell detects focus lost
JOptionPane displayed and user selects action
JOptionPane closes and cell's value set
Button's actionListeners called
Instead, this is happening:
User edits cell
User clicks button
Cell detects focus lost
JOptionPane displayed and user selects action
Button's actionListeners called
JOptionPane closes and cell's value set
Is it possible to postpone the button's action events until after the JOptionPane has closed?
From other threads, I've read that JDialog does some magic to ensure event dispatching continues so the Dialog itself can handle events.
From what I gather, you don't want the button's action-listener to activate at all, until AFTER the user selects the correct value from the JOptionPane.
To me it seems like the solution would be to set up a 'disabled' flag which goes up once the focusLost is triggered. Once the selection is made, the disabled flag goes down. When the button action is triggered, it checks if the form is disabled; if it is, it does nothing. If it isn't it continues as normal.
Notice the button event won't go automatically once the user selected something in the JOptionPane, but instead he will have to click the button again. To me this seems like better functionality then having the button 'clicked' again for him after he is required to change the form.
Put your validation logic inside TableCellEditor#stopCellEditing(), showing your dialog and returning false if the value is not valid.
To automatically stop table editing on focus lost, use table.putClientProperty("terminateEditOnFocusLost", true);, but I don't think that will stop the buttons action listener from running. Instead I usually stop the table edit in the actionPerformed and do nothing when false is returned (or cancel editing when appropriate, for example if the action is to delete that table row).
I am new to Java and am developing a java swing application.
The main frame (JFrame) has a text box and an OK button. There is some long processing to be done when the focus from the text box is lost as well as different long processing when the OK button is clicked. Now if the user enters a value in the text box and clicks the OK button directly, ideally, first the focus lost event is fired and then the event on the OK button. The problem is that while the focus lost event is running a joption frame comes up asking the user for some input, but even before user enters the input here, the OK button event starts executing leading to problems in the application. How can I serialize the event calls.
Any help will be appreciated.
Your problem lies within the concept of the Event Dispatch Thread. For long running work loads, check out the SwingWorker class.
I have an application in which you can do a right mouse button press and drag (as well as a left press and drag for different operations). However, when running this on linux, it seems that popup menus are triggered by a mousePressed and not a mouseReleased. This is resulting in every time I press the right mouse button to perform a drag, the popup menus are triggered (unlike windows, where it is mouseReleased).
Any thoughts on how to work around this?
thanks.
EDIT: Posting code
Code for popup menu
// this is called from mousePressed and mouseReleased
if (e.isPopupTrigger() && !e.isConsumed()) {
// show the popup menu
}
This code is what is called on the right mouse press/drag (this is 3rd party code, but it is open source so I can change as needed)
// this is called on all mouse events
if (buttonAction.mouseButton != 0)
{
// handle the event
}
Yes, use isPopupTrigger(), as shown here.
Addendum:
it appears isPopupTrigger is triggered on mousePressed in linux.
Yes, it's the same on Mac OS X. You have to call isPopupTrigger() from both mousePressed() and mouseReleased(). There's a related example in GraphPanel.
MouseEvent.isPopupTrigger(). Returns whether or not this mouse event is the popup menu trigger event for the platform.
edit - : You need to make the check in both mousePressed for linux, and mouseReleased for windows.
I think the correct procedure in your case should be to unify where and when to show the popup. As a drag event, if exist, follows a press event you should avoid writting logic for showing the popup in the press event (and then also write logic in the press event for showing the popup). Some users feel good navigating the popup while holding the popup button, and some other users just don't care or don't know. But in your case, you won't be able to navigate the popup menu while dragging without adding extra code.
My way would we to manage the logic to always show the popup on the release event. Entering a release event after a drag should be enough information to know that the popup shouldn't be visible. And of course, always if you can change and modify the source.
I writing an application which controls another application by using the keyboard only. To more concrete, the application simulates key presses and mouse clicks when a certain key is pressed on the keyboard. For example, pressing on the 'x' key simulates a mouse click on the [X] in the rop right corner, followed by a little sleep of 2 seconds and an 'enter' to confirm the exit dialog. Pretty easy. I am developing this application in Java.
Sending a key press or a mouse click is very easy with java.awt.Robot. I am facing one little problem. Say I have configured a key which will click somewhere on the screen. The problem is that consecutive key presses aren't catched anymore, as my application lost its focus caused by the mouse click outside it's window.
My question now is: what is the best way to be sure that my main application keeps the focus? Is there a way to focus my application again after the key presses and mouse clicks are sent out? Is there a better way?
Thanks in advance.
If your application lost the focus. because you or your Robot clicked to somwhere else, the Robot must click on the application again before sending a new key. In c/c++ you could force the focus to the application (a non-trivial task), not in Java!
You might want to take a look at Component.requestFocus() to see if can do what you want.
Be aware however that window focusing has very platform dependent behaviour, so you will probably need to do quite a bit of testing to ensure that your code does what you want in all circumstances.
I managed a way to prevent applications from losing all focus in Java.
By placing a WindowFocusListener on the frame (or dialog) and calling setVisible(false) followed by setVisible(true) in windowLostFocus the component will re-appear as soon as it is dissapears (not the prettiest solution but it does work).
By then calling component.requestFocus() your robot should be able to continue where it left off