-1

How can i keep the icon always alive, on click it does not get reloaded.

public static class SoftButton extends Button 
{
    private Image image;

    public SoftButton() 
    {            
        setLabel("test");
        setBackground(Color.red);
    }

    public void paint(Graphics g) 
    {  
        super.paint(g);
        image = Toolkit.getDefaultToolkit().getImage("/tmp/world.gif");
        g.drawImage(image, 0, 0, this);
    }

} 
  • Why are you using `AWT` components in the first place? Also, you may want to check out [An Image Button Class](http://www.apl.jhu.edu/~hall/CWP-Chapter13/CWP-Chapter13.3.html). – Moonbeam Jul 17 '11 at 22:32
  • 2
    -1, You've already been given an answer to this question: http://stackoverflow.com/questions/6718944/how-to-make-awt-button-and-use-imageicon-icon/6719006#6719006. And yes the solution is far more complicated then the code you posted so don't expect us to write the code for you when you already have an answer. – camickr Jul 17 '11 at 22:48

1 Answers1

1

Create a local variable in which you store the Icon. Like you almost did:

public static class SoftButton extends Button 
{
    private Image image;

    public SoftButton() 
    {            
        setLabel("test");
        setBackground(Color.red);
        // Load the icon once in the constructor:
        image = Toolkit.getDefaultToolkit().getImage("/tmp/world.gif");
    }

    public void paint(Graphics g) 
    {  
        super.paint(g);
        g.drawImage(image, 0, 0, this);
    }

} 
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • I don't think this will resolve OP's problem, although it's good advice. – Moonbeam Jul 17 '11 at 22:46
  • 1
    @Martijn Courteaux: This way still does not work. First it shows icon. On click the icon goes away, and never comes back. Its same like my posted code still. –  Jul 18 '11 at 07:45