Change value when mouse is clicked - java

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

Related

Java Swing JPanel: How to forward all method calls to a child JPanel?

I want to create a JPanel that overlays a button in the top-right corner but otherwise behaves like any other JPanel. The only solution I could come up with is something like this:
public class MyJPanel extends JPanel {
private final JPanel realPanel = new JPanel();
private final Button button = new JButton();
public MyJPanel() {
super(new OverlayLayout());
...layout the realPanel and the button on top of it...
}
... implement all public JPanel methods and delegate to "realPanel"...
}
But for this to work, I'd have to override a whole bunch of methods and delegate them to "realPanel". Is there an easy/easier way than having MyJPanel implement dozens of methods that do nothing but call the same method on the child panel?
Swing mouse events are hit-tested and forwarded to the component below the mouse. You need either:
implement those listeners (oh and also translate mouse event coordinates which are relative to the topmost panel)
instead of using a panel, decorate the button by customizing its painting (e.g. override paintComponent, or implement a custom LAF). In this case, the customization is still the same component, so the button will receive all the mouse events.
PS: It is always a good idea to explain why you wish to achieve a goal, instead of just pushing for achieving a certain solution. There are more solutions if the real problem is understood.

mouseListener - Implements vs addMouseListener

I have a JTable and I want to detect a single click and double click on a row. I recognize I need to create a mouseListener. However I have seen examples that extend a JTable and implement a mouseListener as part of the JTable definition, as well as create just a mouse listener and then simply addMouseListener to the JTable. What is the behavior difference and which is the preferred implementation if any? TIA.

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

how to get notification of mouse going out of components in Java

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

How can I capture all mouse events in a JFrame/Swing?

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.

Categories