Mouse Events in a JPanel (Java)
Solved
csi_bern
Posted messages
72
Status
Member
-
danimo -
danimo -
Hello,
I currently have two classes, and here is a summary:
and
(the main method was removed)
My question is the following: why when I click in my window, which contains my JPanel, does the requested message via println not appear in the console? I set this println as a test because all the actions I wanted to capture through mouse clicks are not working!
Please help
Thank you in advance
Configuration: Windows XP / Internet Explorer 8.0
I currently have two classes, and here is a summary:
import java.awt.BorderLayout; import javax.swing.JFrame; public class Fenetre extends JFrame { private panImage panGauche = new panImage(); public Fenetre(){ this.setSize(300, 300); this.getContentPane().add(panGauche, BorderLayout.CENTER); this.setVisible(true); } } and
import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JPanel; public class panImage extends JPanel { public void PanImage() { this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { System.out.println("clicked " + e.getX()); } }); } public void paintComponent(Graphics g) { g.setColor(Color.black); g.fillRect(50, 50, 50, 50); } } (the main method was removed)
My question is the following: why when I click in my window, which contains my JPanel, does the requested message via println not appear in the console? I set this println as a test because all the actions I wanted to capture through mouse clicks are not working!
Please help
Thank you in advance
Configuration: Windows XP / Internet Explorer 8.0
4 answers
Re,
I need more information about your 2 classes...
First of all, it would be better to have classes whose names start with a capital letter.
-Is public class Fenetre extends JFrame {class 1?
-And public class PanImage extends JPanel {class 2
-How do you call class 2 from class 1?
-You said "Wouldn't it be possible to insert the listener directly in class 2?" That's what you do, right?
-It would be good if you could give us more code (especially concerning class 2)
Thank you...
Dan
I need more information about your 2 classes...
First of all, it would be better to have classes whose names start with a capital letter.
-Is public class Fenetre extends JFrame {class 1?
-And public class PanImage extends JPanel {class 2
-How do you call class 2 from class 1?
-You said "Wouldn't it be possible to insert the listener directly in class 2?" That's what you do, right?
-It would be good if you could give us more code (especially concerning class 2)
Thank you...
Dan
Hi Dan,
Thank you for your reply. Unfortunately, when I call my class (class 2) from another one (class 1), the click system does not work.
However, I can put a listener on class 2 in class 1, but eventually I will "overload" my class 1. Would there be a way to insert the listener directly in class 2?
Thank you for your reply. Unfortunately, when I call my class (class 2) from another one (class 1), the click system does not work.
However, I can put a listener on class 2 in class 1, but eventually I will "overload" my class 1. Would there be a way to insert the listener directly in class 2?
Hi Dan,
Thank you for your replies. Java is still a bit tough for me; I've just started getting into it.
First of all, you are right about the class names; that was my mistake. Additionally, Fenetre is indeed class 1 and PanImage is class 2. To call class 2, I declare the variable as "private PanImage panGauche = new PanImage()," (I do the same with a second variable called panDroit). Then, I use "panPrinc.add(panGauche, null);" (panPrinc being a JPanel setLayout(null)). panGauche is positioned in panPrinc using setBounds.
Actually, I mentioned that the listener in class 2 wasn't working, at least not from class 1. And that’s really the problem. I would send you my full code, but it’s starting to get substantial. Plus, I have made significant modifications since my last post to use a listener in class 2 from class 1 (I hope that makes sense). So in class 1, I wrote "panGauche.addMouseListener(...);".
If you have some time, I would appreciate your guidance. I would really like to get this working, as it would simplify my code significantly and make it easier to understand. But for now, it works, and that’s the main thing. If needed, I could send you the source files by email.
Thank you for your replies. Java is still a bit tough for me; I've just started getting into it.
First of all, you are right about the class names; that was my mistake. Additionally, Fenetre is indeed class 1 and PanImage is class 2. To call class 2, I declare the variable as "private PanImage panGauche = new PanImage()," (I do the same with a second variable called panDroit). Then, I use "panPrinc.add(panGauche, null);" (panPrinc being a JPanel setLayout(null)). panGauche is positioned in panPrinc using setBounds.
Actually, I mentioned that the listener in class 2 wasn't working, at least not from class 1. And that’s really the problem. I would send you my full code, but it’s starting to get substantial. Plus, I have made significant modifications since my last post to use a listener in class 2 from class 1 (I hope that makes sense). So in class 1, I wrote "panGauche.addMouseListener(...);".
If you have some time, I would appreciate your guidance. I would really like to get this working, as it would simplify my code significantly and make it easier to understand. But for now, it works, and that’s the main thing. If needed, I could send you the source files by email.
Hello Dan,
I need to do something that resembles the code you sent me.
In class 2, I display all the points that have been clicked (these are recorded in an ArrayList).
In class 1, I insert 2 objects of class 2. So, when I click on one of these objects, the new point should display at the click location in addition to the previous points.
I've managed to achieve that, everything works, but the only problem is that I can't understand why "this.addMouseListener(new MouseAdapter()" in class 2 is not working.
Actually, reviewing my code (several days later and with a clear head), I wonder if it's not due to the fact that I set a void in the constructor of the second class and it has a capital letter difference (which you had already pointed out before): "public void PanImage()".
Unfortunately, I forgot my computer today, so I won’t be able to test before this evening. If it turns out to be just that, I'll let you know.
Thanks again for the time spent on my issue.
I need to do something that resembles the code you sent me.
In class 2, I display all the points that have been clicked (these are recorded in an ArrayList).
In class 1, I insert 2 objects of class 2. So, when I click on one of these objects, the new point should display at the click location in addition to the previous points.
I've managed to achieve that, everything works, but the only problem is that I can't understand why "this.addMouseListener(new MouseAdapter()" in class 2 is not working.
Actually, reviewing my code (several days later and with a clear head), I wonder if it's not due to the fact that I set a void in the constructor of the second class and it has a capital letter difference (which you had already pointed out before): "public void PanImage()".
Unfortunately, I forgot my computer today, so I won’t be able to test before this evening. If it turns out to be just that, I'll let you know.
Thanks again for the time spent on my issue.
Hi,
Actually, yes, the problem was with the constructor because it works now. If it's not necessarily about void, it must have been that I had a capitalization difference between the class and the constructor.
It's these kinds of small mistakes that you don't notice that sometimes make me hate programming!
Actually, yes, the problem was with the constructor because it works now. If it's not necessarily about void, it must have been that I had a capitalization difference between the class and the constructor.
It's these kinds of small mistakes that you don't notice that sometimes make me hate programming!
Hello,
I'm sharing this small class (which is an excerpt from one of my programs) that shows how to get the coordinates of a click.
Best regards,
Dan
I'm sharing this small class (which is an excerpt from one of my programs) that shows how to get the coordinates of a click.
import javax.swing.*; import java.awt.*; // including Toolkit and Graphics import java.awt.event.*; public class ClickCoordinates extends JFrame { //private Image img; private Color lightYellow = new Color(255, 255, 212); private int x, y; private int x1, y1; // for ... (not yet used) private int xClick, yClick; private boolean click = false; private static Font font10; public ClickCoordinates() { super("Click in the frame to get the click coordinates on the console."); // title setSize(538, 770); // frame dimensions (resizable by default) setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); addMouseListener(new MouseHandler()); } public void paint(Graphics g) { // Graphics of the coordinates: super.paint(g); Polygon rectangle2 = new Polygon(); // black rectangle rectangle2.addPoint(xClick -20, yClick -12); // upper left point rectangle2.addPoint(xClick +22, yClick -12); // upper right point rectangle2.addPoint(xClick +22, yClick +14); // lower right point rectangle2.addPoint(xClick -20, yClick +14); // lower left point g.setColor(Color.black); // background of the rectangle g.fillPolygon(rectangle2); // filling g.drawPolygon(rectangle2); // display // drawing the coordinate graph (in white) g.setFont(font10); g.setColor(Color.white); g.drawString(Integer.toString(yClick), xClick -19, yClick +4); // y value of the click g.drawString(Integer.toString(xClick), xClick -3, yClick -4); // x value of the click // x-axis arrow without tip g.drawLine(xClick -2, yClick -1, xClick +18, yClick -1); // x, y points left... and right // tip of this arrow g.drawLine(xClick +16, yClick -3, xClick +22, yClick -1); // x, y points left... and right g.drawLine(xClick +16, yClick +2, xClick +22, yClick -1); // x, y points left... and right // y-axis arrow without tip g.drawLine(xClick, yClick -3, xClick, yClick +9); // x, y points up... and down // tip of this arrow g.drawLine(xClick -2, yClick +6, xClick, yClick +12); // x, y points up... and down g.drawLine(xClick +2, yClick +6, xClick, yClick +12); // x, y points up... and down } public static void main(String[] args) { font10 = new Font("SansSerif", Font.BOLD, 10); new ClickCoordinates(); } private class MouseHandler extends MouseAdapter { public void mouseClicked(MouseEvent evt) { click = true; /*System.out.println("\nL 3260 mouseClicked(MouseEvent evt)\n" + " " + "w/h click/clickedIn/trueFalse[icop]/figure :\n" + " " + w + "/" + h + " " + click + "/" + clickedIn + "/" + trueFalse[icop] + "/" + figure); */ xClick = evt.getX(); yClick = evt.getY(); System.out.println("L 40 xClick/yClick: "+ xClick + "/" + yClick); repaint(); // to display the coordinate graph } } } Best regards,
Dan