File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Changing The ImageIcon Of a JButton With a click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Changing The ImageIcon Of a JButton With a click" Watch "Changing The ImageIcon Of a JButton With a click" New topic

Changing The ImageIcon Of a JButton With a click

Koh Khai Huat
Ranch Hand

Joined: Aug 05, 2005
Posts: 100
Hi guys,

I am working in a applet program and it require the user to click the button and then change the button's image to a different image how do u go about doing that. I am open to all suggestion and advice on how i am going to slove this problem, Thanks in advance


if (!WorkHard){
Vasu Reddy
Ranch Hand

Joined: Sep 13, 2005
Posts: 32

Please go through the below program.

import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;

public class ActionExample extends Applet implements ActionListener

private JButton testButton;
private int test =1;
private ImageIcon testButtonIcon2;

public void init()
ImageIcon testButtonIcon1 = new ImageIcon("test1.gif");
testButtonIcon2 = new ImageIcon("test2.gif");

setLayout(new FlowLayout());
// testButton = new JButton("Test");

testButton = new JButton(testButtonIcon1);



public void paint(Graphics g)

if(test != 1) {


public void actionPerformed(ActionEvent evt)

if (evt.getSource() == testButton)

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Just have an action listener that sets the icon. No need to override paint:

[ February 08, 2006: Message edited by: Jeff Albertson ]

There is no emoticon for what I am feeling!
I agree. Here's the link:
subject: Changing The ImageIcon Of a JButton With a click
jQuery in Action, 3rd edition