1

I have some code for a JButton:

 solutionButton = new JButton("Solution");
 solutionButton.setBorderPainted( false);
 solutionButton.setContentAreaFilled( false );
 solutionButton.setFocusPainted( false);
 solutionButton.setFont( new Font("Arial",Font.BOLD,16));
 solutionButton.setForeground( new Color(80,21,25));
 solutionButton.setRolloverIcon( 
         new ImageIcon(getClass().getResource( "images/game.png")));
  solutionButton.setRolloverEnabled( true );
 add(solutionButton);

If I simply setIcon, it works fine, I see the icon. If I do the above and try to set a rollover icon, I do not see any icon when I mouseover the button.

What am I doing wrong?

Thanks

mKorbel
  • 109,525
  • 20
  • 134
  • 319
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 1
    Compare this [example](http://stackoverflow.com/questions/4170134/java-swing-jcomponent-size/4170233#4170233). – trashgod Nov 15 '11 at 02:41

1 Answers1

5

Point at the Moon!

import java.awt.Image;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;

class TestRolloverIcon {

    public static void main(String[] args) throws Exception {
        URL url1 = new URL("http://pscode.org/media/citymorn1.jpg");
        URL url2 = new URL("http://pscode.org/media/citymorn2.jpg");
        final Image image1 = ImageIO.read(url1);
        final Image image2 = ImageIO.read(url2);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JButton button = new JButton("Point at the Moon!");
                button.setIcon(new ImageIcon(image1));
                button.setRolloverIcon(new ImageIcon(image2));

                JOptionPane.showMessageDialog(null, button);
            }
        });
    }
}

Is there a way to do it without first setting an icon, I only want a rollover icon not an icon too.

Point at space (there's a lot of it)!

import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;

class TestRolloverIcon {

    public static void main(String[] args) throws Exception {
        URL url2 = new URL("http://pscode.org/media/citymorn2.jpg");
        final BufferedImage image2 = ImageIO.read(url2);
        final BufferedImage image1 = new BufferedImage(
            image2.getWidth(),image2.getHeight(),BufferedImage.TYPE_INT_ARGB);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JButton button = new JButton("Point at space (there's a lot of it)!");
                button.setIcon(new ImageIcon(image1));
                button.setRolloverIcon(new ImageIcon(image2));

                JOptionPane.showMessageDialog(null, button);
            }
        });
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Is there a way to do it without first setting an icon, I only want a rollover icon not an icon too. – jmasterx Nov 15 '11 at 02:48