Results 1 to 4 of 4
  1. #1
    shane5317 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Unhappy how to add event Listener to Image

    Hi all,
    I am Beginner of Java.I having problems with the event listener. I have created image and successfully display on the screen. My problem is i don't have any ideas how to add the event listener onto image. Any tutorials?
    For example,

    JButton button = new JButton("click me");
    button.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {
    //Execute when button is pressed
    JOptionPane.showMessageDialog(null,"You clicked the button");
    }
    });


    How about image??I have image like this
    Image img;
    MediaTracker mt = new MediaTracker(this);
    img = Toolkit.getDefaultToolkit().getImage("image1/Sleet.png");
    mt.addImage(img,0);
    //what should i do next

    i want to perform just like button??can i do that?Anyone please help me!!:(

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You can put the image into an ImageIcon and then put that on a JButton that has the code you desire to be activated in its ActionListener. Alternatively if you don't want it to be a button, add the ImageIcon to a JLabel and add a MouseListener to the JLabel that does the code you desire in the mousePressed method.

  3. #3
    shane5317 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    thanks for the idea. =D

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. JColorChooser modeless - Event Listener problem
    By siamino in forum New To Java
    Replies: 1
    Last Post: 04-14-2009, 12:39 AM
  2. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  3. Mouse Event + Image Thresholding
    By ojmayolebron in forum AWT / Swing
    Replies: 0
    Last Post: 03-27-2009, 12:17 AM
  4. Replies: 5
    Last Post: 11-02-2008, 06:41 AM
  5. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •