user650679
user650679

Reputation: 27

adding action listener to an image

I insert an image to a JPanel. I write this code.

public void paint(Graphics g)
{

    img1=getToolkit().getImage("/Users/Boaz/Desktop/Piece.png");
    g.drawImage(img1, 200, 200,null);

}

I want to add an action listener to that picture, but it does not have an addActionListener() method. How can I do that without putting the image in a button or label?

Upvotes: 0

Views: 14187

Answers (2)

handuel
handuel

Reputation: 21

The easiest way is to put the image into a JLabel. When you use the program, it appears to just be the image, you can't tell its in a JLabel. Then just add a MouseListener to the JLabel.

Upvotes: 2

coobird
coobird

Reputation: 161042

There are a few options.

Use a MouseListener directly in the JPanel

A simple but dirty way would be to add an MouseListener directly to the JPanel in which you overrode the paintComponent method, and implement a mouseClicked method which checks if the region where the image exists has been clicked.

An example would be something along the line of:

class ImageShowingPanel extends JPanel {

  // The image to display
  private Image img;

  // The MouseListener that handles the click, etc.
  private MouseListener listener = new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
      // Do what should be done when the image is clicked.
      // You'll need to implement some checks to see that the region where
      // the click occurred is within the bounds of the `img`
    }
  }

  // Instantiate the panel and perform initialization
  ImageShowingPanel() {
    addMouseListener(listener);
    img = ... // Load the image.
  }

  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }
}

Note: An ActionListener can't be added to a JPanel, as a JPanel itself does not lend itself to create what is considered to be "actions".

Create a JComponent to display the image, and add a MouseListener

A better way would be to make a new subclass of JComponent whose sole purpose is to display the image. The JComponent should size itself to the size of the image, so that a click to any part of the JComponent could be considered a click on the image. Again, create a MouseListener in the JComponent in order to capture the click.

class ImageShowingComponent extends JComponent {

  // The image to display
  private Image img;

  // The MouseListener that handles the click, etc.
  private MouseListener listener = new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
      // Do what should be done when the image is clicked.
    }
  }

  // Instantiate the panel and perform initialization
  ImageShowingComponent() {
    addMouseListener(listener);
    img = ... // Load the image.
  }

  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }

  // This method override will tell the LayoutManager how large this component
  // should be. We'll want to make this component the same size as the `img`.
  public Dimension getPreferredSize() {
    return new Dimension(img.getWidth(), img.getHeight());
  }
}

Upvotes: 5

Related Questions