I am working in application in which i want notification if the mouse has gone out of the JPanel (let's say one of the components) how can this be done ?
one way is by implements MouseMotionListener, another examples here
Call addMouseListener and implement the MouseListener.mouseExited method.
override mouseExited method.
http://www.daniweb.com/software-development/java/threads/128332
Related
Picture of some of the code that I'm trying to work with
I'm trying to make it in Java so that my labels can change what Icon they are displaying, and it seems like an ActionListener is the best way to do it, but I'm still a little confused.
Apparently ActionListener will trigger and perform the thehandler class (according to the tutorial I found) when an event occurs, though how do I declare what the event that triggers ActionListener will be? Also why I am I red lining?
I appreciate the help.
You should try to implement MouseListener instead of the more generic ActionListener interface. This will make you listen to mouse clicks etc.
Difference between Mouse Listener and Action Listener?
I have JTextfield. Now I want to change value when in this component is mouse clicked. For example: score (2 big JTextField) and when I click to one of these field it increase the value from 0:0 to 1:0.
Should I implement MouseListener or there is some easy way how I can do this? In mouse listener I need override just one method mouseClick and other method will be empty.
And another question: when should I implement MouseListener? e.getButton() return always 1 for left button and 3 for right button?
Should I implement MouseListener or there is some easy way how I can do this? In mouse listner I need override just one method mouseClick and other method will be empty.
Use a MouseAdapter.
An abstract adapter class for receiving mouse events. The methods in this class are empty. .. Extend this class to create a MouseEvent (including drag and motion events) or/and MouseWheelEvent listener and override the methods for the events of interest.
Now I want to change value when in this component is mouse clicked
JTextComponents are Focusable, look for FocusListener
Implementing MouseListener on your class is one way to do it, but if you just want to react to clicks, it's easier to use an anonymous class extending MouseAdapter
textField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
// do your thing here
}
});
As for the second question, the API documentation quite nicely documents the return values of MouseEvent.getButton().
I've just managed to get KeyListener to work in an empty application, but then I tried to implement it in my application and it simply doesn't work, no matter how much I enter keys!
A friend told me it's because I have buttons (JButton) on the application (and I implement the actionPerformed method,) can anyone explain to me why this is happening (and how to fix it) ?
EDIT:-
Yes, the problem is about focus, and I found the solution in some forums, and the solution is very simple. It`s by adding:
setFocusable(true); after, setVisible(true); in the class that extends JFrame.
To wich component are you adding the KeyListener? i think that if you want to listen the KeyEvents in the whole calculator you must add it to the Container in wich the buttons are.
But i belive that only the component who has the focus recive the KeyEvents, so that i dont know if the container can has the focus or if the events are promoted to their parent if the focus is on the buttons of the calculator.
You can solve this adding a keyListener to the container doing the stuff (printing the characters i suposse), and to the buttons and the textfield, promoting the event to it's container
I have a JFrame that has a large number of changing child components. (Many layers) Is there any way to add a listener for all mouse events? Something like KeyEventDispatcher?
Use an AWTEventListener to filter out the MouseEvents:
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
{
public void eventDispatched(AWTEvent e)
{
System.out.println(e);
}
}, eventMask);
You could add a GlassPane over your entire JFrame, add a MouseInputAdapter to it to grab all possible mouse events, and then use [SwingUtilities.getDeepestComponentAt()][3] to get the actual component and [SwingUtilities.convertMouseEvent()][4] to delegate the mouse event from the glass pane to the actual component.
However, I'm not sure of the performance impact of this - unlike KeyEventDispatcher, which just needs to fire an event whenever a key is pressed, multiple events are generated as the user moves the mouse - and unlike KeyEventDispatcher, you need to re-send the event to the lower component for it to handle it.
(Sorry - stackoverflow isn't handling the links to the SwingUtilities methods correctly... links are showing below rather than in the text.)
[3]: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html#getDeepestComponentAt(java.awt.Component, int, int)
[4]: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html#convertMouseEvent(java.awt.Component, java.awt.event.MouseEvent, java.awt.Component)
You have to use JFrame's glassPane:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html#getGlassPane()
Just get the glass pane of a JFrame with frm.getGlassPane() and use addMouseListener() on it to capture all mouse event inside the window.
Implement all mouse-related listeners in a class, and register that class as the handler for all mouse related events
Mouse Related interfaces would be
MouseListener
MouseMotionListener
MouseWheelListener
You might want to implement a subclass of MouseAdapter, an abstract class that provides empty implementations of all of the methods defined in the Mouse*Listener Interfaces. Once you do that, you can register it with your child components as a MouseListener when they are created. As you indicate that your components are 'changing,' you will want to make sure the you also unregister your listener if you hope to release your components during the lifecycle of your JFrame.
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.