AleMal
AleMal

Reputation: 2087

Java trap events from subclasses obj

In my class i would like to add to main JPanel others JPanel obj dinamically created within a subclass and trap mouse events knowing exactly witch obj generate it. I try to do this by create a subclass DoSquare (JPanel extend) , add listener, create objs and draw Graphics of that subclass, but when i run program and click onto one of DoSquare obj created, the e.getSource() return always the SubObj object instead the element clicked on. How can i discriminate exactly the component selected in Listener events?

Here my code:

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

public class SubObj extends JPanel {

 Listener listener = new Listener();
 DoSquare firstQ = new DoSquare(100, 50);
 DoSquare secondQ = new DoSquare(200, 70);

 public static void main(String[] args) {

    JFrame panel = new JFrame();
    SubObj content = new SubObj();
    panel.setContentPane(content);
    panel.setFocusable(true);
    panel.setSize(400, 400);
    panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.setVisible(true);

 }

 SubObj() {
    addMouseListener(listener);
    addFocusListener(listener);

 }

 public void paintComponent(Graphics g) {
    super.paintComponent(g);


    firstQ.draw(g, Color.BLACK);
    secondQ.draw(g, Color.CYAN);
 }

 private class DoSquare extends JPanel {

    private int coordX;
    private int coordY;
    Listener squareLis = new Listener();

    DoSquare(int X, int Y) {
        addMouseListener(squareLis);
        addFocusListener(squareLis);
        coordX = X;
        coordY = Y;
    }

    void draw(Graphics g, Color c) { // Draw the square

        g.setColor(c);
        g.fillRect(coordX, coordY, 50, 50);
    }


}

 public class Listener implements MouseListener, FocusListener {
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {
        System.out.println(e.getSource());

    }
    public void mouseReleased(MouseEvent e) {}

    public void focusGained(FocusEvent e) {}
    public void focusLost(FocusEvent e) {}
 }

}

Thanks in advance

AM

Upvotes: 1

Views: 51

Answers (1)

lkq
lkq

Reputation: 2366

In your SubObj class's constructor, you should do:

SubObj() {
    firstQ.addMouseListener(listener);
    secondQ.addFocusListener(listener);
}

And delete the squareLis variable in your DoSquare class.

UPDATE

public class SubObj extends JPanel {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                SubObj content = new SubObj();
                frame.setContentPane(content);
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    SubObj() {
        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println(e.getSource());
                System.out.println("click from " + ((TestPanel) e.getSource()).getId());
            }
        };

        JPanel testPanel1 = new TestPanel("1");
        testPanel1.setPreferredSize(new Dimension(200, 200));
        testPanel1.setBackground(Color.BLACK);
        testPanel1.addMouseListener(mouseAdapter);
        add(testPanel1);

        JPanel testPanel2 = new TestPanel("2");
        testPanel2.setPreferredSize(new Dimension(100, 100));
        testPanel2.setBackground(Color.RED);
        testPanel2.addMouseListener(mouseAdapter);
        add(testPanel2);
    }

    private class TestPanel extends JPanel {
        private String id;

        TestPanel(String id) {
            this.id = id;     
        }

        public String getId() {
            return id;
        }
    }
}

The code I wrote above is equivalent to what you want to achieve, and it worked the way you want. I don't use your code since I'm not sure what exactly you want it to be. My code is for demonstrating how two sub-panels respond the mouse events when gets clicked, that is our focus for this problem.

Upvotes: 2

Related Questions