Add actionlistener to jpanel - java

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..

Related

Java button mouseclicked joptionpane

I am extending the JButton class and using an ImageIcon as my button background. I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button. This all works fine but the problem is that when the button is clicked a JOptionPane is displayed asking the user "Are you sure" while the option pane is displayed the buttons MouseClicked, mouseExited or mouseReleased methods are not executed so it looks like the button is stuck pressed until the use deals with the option dialog. Removing the dialog is not an option. Is there a way to get the mouse events executed before or during the dialog displaying?
I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button.
Read the AbstractButton API. There are methods that allow you to specify the Icon for these situations. For examples setPressedIcon().
it looks like the button is stuck pressed until the use deals with the option dialog.
Not sure if the ButtonModel state has been changed before the option pane is displayed or not. Anyway you can try using:
button.paintImmediately(...);
before the option pane is displayed. If the ButtonModel state has been updated the button should be repainted in its normal state. Note, this method should rarely be used since it can affect painting performance.

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

Add 'inactive' mouse listener

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.

Retaining Focus for KeyboardListener

I have a JFrame which contains most everything in the application. It has a KeyListener attached, and it also has several buttons and textfield on it. The problem it, when a button is clicked or a textfield is selected, it gets focus and shortcuts don't work. Of course, one can tab out of them, but to do this you must tab through EVERYTHING (each button, each textfield) before giving the window focus again.
Is there a sensible way to only require one tab to return focus to the frame from the textfield, and no tabs to return focus to the frame from a button click?
A WindowListener doesn't seem like the best way to do this, but if it's the only way I suppose I can forge forward there.
Thanks in advance!
It has a KeyboardListener attached
I have never heard of the KeyboardListener class so I can only guess what you are trying to do.
My guess is that you should NOT be using a listener of any kind.
Instead you should be using Key Bindings.
If you only need this for the textfield, you can add a keyListener to the textfield and when the user presses tab use yourJFrame.requestFocus(). Otherwise refer you may want to use a window manager or a key map.

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