1

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)

`

timekeeper
  • 698
  • 15
  • 37
  • Have you already tried to cast the object to a JPanel? – ZeusNet Dec 28 '16 at 10:43
  • Can you please put the exception stack trace into this question – ZeusNet Dec 28 '16 at 10:47
  • I got that. If you want to figure out where the problem is. Debug your code an look into the e.getSource and determine which type it is. Then you know how you have to cast it – ZeusNet Dec 28 '16 at 11:07
  • This may be helpful : http://stackoverflow.com/questions/7340001/determine-clicked-jpanel-component-in-the-mouselistener-event-handling – Pankaj Verma Dec 28 '16 at 11:13
  • I also thought of adding a boolean as attribute to the classes that extend JButtons and JPanels that would say whether the component is selected or not and would be set to true inside `mouseClicked` cue what I wanna do is move the JButtons from a JPanel to another on the gridlayout cells using xy coordinates (cuz my jframe contains a two-dimensional array of jpanels) so I will go through the entire Gridlayout cells using a loop, search for the selected button, take its coordinates, same for the selected panel, take its coordinates, finally put the button in the panel. what do you think ? – Akram Bendoukha Dec 28 '16 at 11:41
  • Post a [mcve] and relate the lines in the stack trace to it. – user1803551 Dec 28 '16 at 11:43

2 Answers2

0

Hope, this will help

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Pion extends JButton
{
    public Pion (String title)
    {
        super(title);
        addMouseListener(new MouseListnerAdapter());
    }
}

class MouseListnerAdapter extends MouseAdapter
{
    @Override
    public void mousePressed(MouseEvent e) {
        Object o = e.getSource();

        if(o instanceof JButton)
        {
            JButton b = (JButton) o;
            b.setBounds(e.getX() + b.getX(), e.getY() + b.getY(), 30, 30);
        }
    }
};


public class Test extends JFrame {
    public Test() 
    {
        super("Test");        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(2,1));


        JPanel p1 = new JPanel();
        Pion pb1 = new Pion("button1");
        p1.add(pb1);
        add(p1);

        JPanel p2 = new JPanel();
        Pion pb2 = new Pion("button2");
        p2.add(pb2);
        add(p2);        

        pack();
        setSize(240, 240);
        setLocationByPlatform(true);
        setVisible(true);
        addMouseListener(new MouseListnerAdapter());

    }

    public static void main(String[] args)throws Exception 
    {
        new Test();
    }
}
Pradnyarani
  • 302
  • 1
  • 8
  • Tried it, the if condition seems to never be satisfied. – Akram Bendoukha Dec 28 '16 at 15:15
  • Tried it, the if condition seems to never be satisfied, I added another if block to try : if(o instanceof CurrentClass){System.out.println(...);} this is the only block of code that has been executed. so as a conclusion e.getSource() always gives back a reference to an object of the current class on which the addmouselistener() method has been called I guess. So I still don't know how to get a reference to a specific component. thanks a lot tho :) – Akram Bendoukha Dec 28 '16 at 15:21
0
java.lang.ClassCastException: PlateauColorLine cannot be cast to javax.swing.JPanel

The source of the event is the JPanel since you added the MouseListener to the panel.

If you want the button at that location of the panel then you need to search the panel to find the component at that location on the panel.

Something like:

JPanel panel = (JPanel)e.getSource();
Component c = panel.getComponentAt(e.getPoint());

if (c != null)
    Pion p = (Pion)c;
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Which coordinates does getComponentAt(....) method take as a parameter, is it the getXOnScreen(), getYOnScreen() coordinates or is it relative to where the event occurs or something ? – Akram Bendoukha Dec 28 '16 at 16:30