Ignore mousePressed - java

I have a mouseListener on my Component and I need do the action on mousePressed in any case except one: if the focus is on another window and user clicks on my Component.
How can I ignore the mousePressed for Component if the focusOwner was another window before MouseEvent was fired? The FocusListener.focusGained and checking the focusOwner at the click moment can't help.
UPD: My task is to detect the case when focusGained fired on cause of mousePressed.

Use a WindowListener and handle the windowActivated and windowDeactivated events to register/deregister your MouseListener.
You may need to place the adding of the MouseListener in a SwingUtilities.invokeLater() to make sure the listener is added after the window has focus.

I had a similar Problem.
If you implement and register FocusListener you can ask in the focusGained method FocusEvent.getOppositeComponent() and detect if the user has changed the window. After that you can register the Mouslistener and unregister it in the focusLost method.

Related

How to know when the gained focus on the first component is initiated by default when parent component became visible or by users actions

I have a popup component which has a first component JTextComponent and I need to recognize in FocusListener where from the focus gained to this component: when popup appears and the component get focus by default or user clicks on it. I didn't find the way to do it using FocusEvent.
Assuming a general use-case, what exactly is it that is tripping you? In my view:
Write the code you wish to execute when focus is gained in a method, say onFocusGained(boolean defaultFocus).
Call it from the FocusEvent methods with onFocusGained(true).
Call it from the MouseAdapter methods with onFocusGained(false).

Java GUI Mouse Listener

could you guys help me out.
How do we order the process of Mouselistener?
I mean, I want my mouseEntered and mouseExited working after I click on my one of my JPanel.
If I understand your question, you want to enable a MouseMotionListener only after a component is clicked...
Basically, in your MouseListener's mousePressed method, you would simply add your MouseMotionListener
Now remember, mouse listeners consume events, that is a child component will hide mouse events that occur on it from it's parent container
Take a look at How to write mouse listeners for more details

Difference between ChangeListener and ItemListener

What is the difference between ChangeListener and ItemListener for JCheckBox and JRadioButton? Both of them work fine when they are selected/deselected.
I know that some components doesn't support ChangeListener like the JComboBox. Other than the reason that ChangeListener or ItemListener work for only some components. Is there any difference between them like when are they generated?
Any answer is appreciated. Thanks in advance.
both listeners for JCheckBox work similarly in that both will fire event upon change in state, whether by clicking or toggle by spacebar or programmatically through doClick() method (Similar to mouse click). One major difference though is that JCheckBox’s itemListener can be fired through setSelected(boolean) method which allows one to fire the event based on desired state whereas others will act only after the state is altered. So why is it important ? Consider when application startup, the GUI needed to configure for defined state, and using setSelected will trigger ItemListener. Note that setSelected is exclusive to ItemListener and has no effect on ActionListener. Do not register both ActionListener and ItemListener as both will be fired, landing the component in a random state
ChangeListener is notyfied when there's any change to the button state. ChangeListener is not notified of what has changed, only that the object has changed. Item listener is only notyfied when an item is selected; by user or setSelected method. It's also not true that ChangeListener is not notyfied when setSelected method is invoked. It is the change of the object state.

Problems with the modality of a JDialog (mouse events for main window both fired and buffered)

I made a JDialog and the modality works presumably fine
dialog.setModalityType(JDialog.ModalityType.APPLICATION_MODAL);
dialog.setVisible(true);
But then my problem is:
I´m throwing Jdialog after a jcombobox.setSelection() and I need to click twice in Accept button in order to hide the dialog, because dropdown popup is consuming the first click for closing himself. I fixed it by manually calling jcombobox.hidePopup() before calling the dialog, but I cannot understand if the later is modal, why the mouse events trigger things outside the window?`
My Main window buffers somehow the mouse events, so for those mouse events which are not activated when the modal dialog is drawn (as happens with the previous point), it seems they get buffered and are applied after dialog closure. Is this an expected behavior?
Thank u!
replace jcombobox.hidePopup("doesn't make me sence") with ActionListener or ItemListener added to the JComboBox
add RequestFocusListener by #camickr for setting the FocusOwner correctly
for why reasons are there another MouseListeners, maybe in the case that fird any events to the JComponents that you can't to set Focus correctly

Swing: Adding listener to a component and ALL its decoration?

Can I add a listener (let's say MouseAdapter) to a Swing component
and all it's internal decoration components?
So that when a JInternalFrame is moved by the mouse
(by dragging its window title bar), it would give me following events:
mousePressed event,
mouseDragged event,
mouseReleased event.
Currently, I receive none of the above events when dragging
JInternalFrame.
I hope there is some standardized solution, but I couldn't find any.
EDIT:
Some people suggest using ComponentListener, but that wouldn't do for
me. I need to know, when the user stops dragging (mouseReleasedEvent),
not when the component moves.
Yes, you can add a listener to all a container's components. getComponents and add the listener. You should be able to manage to do this recursively. You can also use ContainerListener to check for adding and removing components.
However, MouseListener and MouseMotionListener behave strangely in that the event normally bubbles up to the parent, but does not do so if a listener is present (how is that for hopeless design?).
Your choices are:
Recursively adding listeners (bad, see above)
Adding listeners to specific components (fragile)
Adding a "glass pane" (a messy hack)
Adding an AWTEventListener to Toolkit (requires permissions)
Pushing an EventQueue and checking through events (doesn't work of Opera and Safari apparently; stops system copy-and-paste and applet dragging from working)
Use ComponentListener?
I found out how it could be done, but something tells me, it's a dirty hack ;)
Well, it works, but who can give me the guarantee that it works everywhere?
// ctor goes here {
InternalFrameUI thisUI = getUI();
((BasicInternalFrameUI) thisUI).getNorthPane()
.addMouseMotionListener(new MyMouseListener());
// }
NorthPane turns out to be the window title bar.
You should probably use a MouseMotionListener instead of a MouseListener.
In the JInternalFrame API documentation, it says:
Generally, you add JInternalFrames to
a JDesktopPane. The UI delegates the
look-and-feel-specific actions to the
DesktopManager object maintained by
the JDesktopPane.
Maybe you should add your listener to the JDesktopPane.
MouseListener/MouseMotionListener wont detect when dragging a JInternalFrame. Your best bet here to detect movement is using a ComponentListener on the JInternalFrame itself.

Categories