Add 'inactive' mouse listener - java

Mr Korbel was dead on, Thank you very much
I have a mouse listener on a JPanel and its working properly. I'm trying to add some rollover support to a JLabel but when I add the mouse listener to the JLabel the JPanel's mouse listener stops receiving events when the label's mouse listener receives events.
I just want to add a mouse listener to the JLabel without blocking the mouse listener of the JPanel under it. Thanks.
Bonus problem! I would like to do getComponentAt(Point p) and get the component on the botton, not the topmost, any suggestions? I have a workaround for this so this part isn't important but I dislike workarounds. Thanks again.

please is your JLabel moveable/dragable, in other cases your question doesn't make me real sence because deepest JComponenet in the hierarchy take Focus / MouseListeners by default, anyway look at SwingUtilities, there are methods can returns relative coordinates to the parent

but when I add the mouse listener to the JLabel the JPanel's mouse listener stops receiving events when the label's mouse listener receives events.
Yes, this is the way mouse event handling works. Only the first component that has a listener will receive the event. It wouldn't be very efficient to keep passing the event up the component hierarchy to each component.
I just want to add a mouse listener to the JLabel without blocking the mouse listener of the JPanel under it.
You can handle this in the mouse listener attached to the label and then redispatch the event to the parent panel. Check out the dispatchEvent(...) of the Component class in the API for more information.

Related

Add actionlistener to jpanel

I would like to know if there is any way to add an ActionListener to a JPanel?
I have no problem adding those to JButtons, but JPanel seems not to have such a method.
Basically I have a bunch of JPanels inside a JFrame with a grid layout and I would like to know if there is any way to know when the user has clicked on one of them.
Any help much appreciated!
An action listener serves to listen to action events. A button triggers an action event when it's pressed and released using the mouse (and not when it's clicked), or when it's pressed using the keyboard (space bar, mnemonic, keyboard shortcut, etc.). It's a high-level event. A mouse click is a more low-level event, which is handled by a MouseListener.
Add a MouseListener to your JPanel if you want to handle mouse clicks.
Aparently you can use AddMouseLitener..
Silly me..

Manage multiple Jlabel's events

I have a left Panel with multiples Jlabels which i use them as buttons to change a Main Panel's content which is layouted with a CardLayout.
I cant work perfectly with these events:
mouseEntered : to make highlight effect to the jlabel
mouseExited : to take off the highlight effect.
mouseClicked : to change the content of the main Panel and start some threads
The problem here that can't found an event or a method tell me that another Jlabel has been clicked so i can stop my threads started in the mouseClicked event,
OR
an event or method tell me that a JPanel in the CardLayout has been displayed or hidden.
Your problem is not finding an appropriate event. I think you are doing this using a visual GUI builder and expect to solve everything out-of-the-box. It's not going to work that way, you will need to write some real code. For example, write a method that you will call from the mouse click listener of each of the three JLabels. Thus you will have arranged for this method to be called for each JLabel click. Then in the method do the appropriate handling. This is just a rough outline, you haven't provided much detail to give any further advice.
It sounds like you need FocusEvents and FocusListeners. These are supported by all JComponents like JPanel, JLabel, and JButton, such as by calling addFocusListener();
Basically a FocusListener can tell you when a JComponent gains focus (such as by clicking on the JComponent) and when it looses focus (such as by clicking on a different JComponent).
Refer to http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/FocusListener.html for further information

How can I do a "mouse out" effect in Java?

I'm trying to mouse over a JButton (javax.swing.JButton) and set the border color, Now I can get this to work I with a mouse move event on the button, I just need to mouse out and I can't seem to find a way to get a mouse out effect to work. Any ideas or methods that could help? Note: MouseExited does not work. At least in my instance.
What about a "mouse over" on the buttons parent Component? Set a flag if the mouse is over the button and if it is over the parent (surrounding) panel and the flag is set, you have something like "mouse out".
Using the mouseExisted method is the way to achieve this effect. If you are having trouble with it, then it is a problem in your code, but you are going down the right path.

My KeyEvents don't get registered while the Timer is firing events. How to fix this?

java noob here. In my Application class, I have a JPanel with focusable set to true and a KeyListener class added to it. I also have a Timer registered with a TimerListener class set up in the Application's main function. The KeyEvents work, but once I press the JButton to call timer.start(), KeyEvents stop firing and only the timer's actionPerformed executes. When timer.stop() gets called, the KeyEvents still don't work. I know I've set this up wrong, but I can't figure out how to fix it. Can anyone help?
but once I press the JButton to call
timer.start(), KeyEvents stop firing
and only the timer's actionPerformed
executes.
KeyEvents are only passed to the component with focus.
If you click on the button, then it has focus, not the panel.
Try resetting focus back on the panel in the button ActionListener code.
Or a better solution is to use Key Bindings. You can set up the key bindings so that the Action will still be invoked when the focus is on the panel or another component on the panel.

Mouse events on JPanel

i have mouselisteners on multiple jpanels. I need to detect if the mouse is pressed when it enters on a different jpanel than the one the event has started from. how can i do that?
Either listen on a JPanel that includes the others or see if using Drag and Drop might suit your needs better.

Categories

Resources