Results 1 to 4 of 4
- 03-10-2011, 12:50 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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!!:(
-
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.
- 03-13-2011, 11:33 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
thanks for the idea. =D
-
Similar Threads
-
JColorChooser modeless - Event Listener problem
By siamino in forum New To JavaReplies: 1Last Post: 04-14-2009, 12:39 AM -
checking for an event during an event
By infinity in forum AWT / SwingReplies: 22Last Post: 04-09-2009, 01:08 AM -
Mouse Event + Image Thresholding
By ojmayolebron in forum AWT / SwingReplies: 0Last Post: 03-27-2009, 12:17 AM -
n00b : Ghost image while using mouseDragged event to move a filled jPanel
By ankitmcgill in forum New To JavaReplies: 5Last Post: 11-02-2008, 06:41 AM -
Listener for SWT event
By Java Tip in forum Java TipReplies: 0Last Post: 01-08-2008, 09:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks