I created a custom JPanel, and want to add a MouseListener to it.
the JPanel is a class, so I am not sure if this is allowed or not.
That was the custom JPanel
This is the listener
I am not sure why the listener is not getting attatched to the JPanel, as it never prints "Good"
I would like to know whether or not this is allowed or not, if not are there any other ways to do this without having to put the JPanel into another class.
-Thanks
the JPanel is a class, so I am not sure if this is allowed or not.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.JPanel; public class thingy extends JPanel{ Listener inp; public Graphics g1; private static final long serialVersionUID = 6049389149892185030L; mainDisp disp = new mainDisp(); public thingy(){ inp = new Listener(); this.setFocusable(true); this.addMouseListener(inp); this.setBackground(Color.red); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Something",100,100); } }
That was the custom JPanel
This is the listener
import java.awt.event.*; public class Listener extends MouseAdapter{ public void MousePressed(MouseEvent ev){ System.out.println("Good"); } }
I am not sure why the listener is not getting attatched to the JPanel, as it never prints "Good"
I would like to know whether or not this is allowed or not, if not are there any other ways to do this without having to put the JPanel into another class.
-Thanks