I wrote this implementation of mouseinputlistener methods
public void mouseDragged(MouseEvent e){
Pion c = (Pion)e.getSource();
c.setBounds(e.getX() + c.getX(), e.getY() + c.getY(), 30, 30);
}
which I implemented in class which is an extension of JFrame
and it contains a gridLayout with different components : JPanel
and some panels contain JButtons
what I need is a method that gives me a reference to the component on which the event occurs whether it's a JPanel
or a JButton
.
I tried by using e.getSource, but obviously it doesn't work, the code compiles, but I get an exception saying that "Plateau ( i.e the current class which extends JFrame
) cannot be cast into a Pion (i.e a class that extends JButton
)" every time I drag the mouse on a component
I tried to implement MouseInputListener to each class that extends a component (Pion and others) but I need to do actions on a JFrame
scale, and that doesn't allow this.
here's the exception I get :
java.lang.ClassCastException: PlateauColorLine cannot be cast to javax.swing.JPanel
at PlateauColorLine.mouseDragged(PlateauColorLine.java:299)
at java.awt.Component.processMouseMotionEvent(Component.java:6564)
at java.awt.Component.processEvent(Component.java:6285)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Window.processEvent(Window.java:2022)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at org.GNOME.Accessibility.AtkWrapper$5.dispatchEvent(AtkWrapper.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
`