Create images clicked on jpanel

How can I add an image (car, land or other) that can be clicked by the user? I want to add them to jpanel with the overridden paint method.

+2
source share
2 answers

Just use JLabel with an icon. Then add a MouseListener to listen for clicks.

JLabel label = new JLabel(yourIcon); // probably an ImageIcon
label.addMouseListener(new MouseAdapter(){
   public void mouseClicked(MouseEvent e) {
     System.out.println("Click at: " + e.getPoint();
   }
});
+4
source

The easiest way is to add an icon in JButton, then you can use the ActionLlistener to handle the mouse click. You can also use:

button.setBorderPainted( false );

to get rid of the border so that it looks like a label.

0
source

All Articles