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.
Related
I have a panel with 6 JButtons and an empty JLabel on the bottom. I want the JLabel to display a description of whichever button I hover over with the mouse through mouseEntered() method.
Is there any sort of method equivalent to getActionCommand() for MouseEvent? I figure if the program is able see which button's string I'm hovering over then I'll be able to change the JLabel.
Edit:
If there is no equivalent method, which doesn't seem to be from what I understand from the docs, is there a way I can use the mouseEntered method for multiple buttons?
I figured I can use getSource to specify which button it's hovering over. Pretty much does exactly what I want it to do.
So, I was just wondering. Say I had a simple game of pong consisting of two JButtons as pongs that move with key input, a smaller JButton for the ball moving in a timer and a text box for a score. When I made this, I had to click on the button before I could move it. Is there a way to make it so that the button is selected by default when the program runs so that I can just press keys to move it straight away without clicking it first? Thanks.
Use the setDefaultButton method of the JFrame's root pane:
myFrame.getRootPane().setDefaultButton(button);
In frames constructor use below code after making button:
this.getRootPane().setDefaultButton(button);
I think you can select one by default with requestFocus().
Something like:
defaultJButton.requestFocus();
You should do that at the initialization or every time you want to restart, reset the state or similar.
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 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
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.