• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Changing The ImageIcon Of a JButton With a click

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

regards,
KKH
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.setBackground(Color.blue);

testButton = new JButton(testButtonIcon1);
add(testButton);

testButton.addActionListener(this);

}


public void paint(Graphics g)
{

testButton.setBackground(Color.blue);
if(test != 1) {
//testButton.setBackground(Color.orange);
testButton.setIcon(testButtonIcon2);
}

}

public void actionPerformed(ActionEvent evt)
{

if (evt.getSource() == testButton)
test++;
repaint();
}

}
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just have an action listener that sets the icon. No need to override paint:

[ February 08, 2006: Message edited by: Jeff Albertson ]
reply
    Bookmark Topic Watch Topic
  • New Topic