2

I got white icon and i want to make difrent color when i go across that icon.

Here is my icon which is white:

hint.setIcon(newjavax.swing.ImageIcon(getClass().getResource ("white.png")));

How can i make when you dragging the mouse over icon to change color?

Luka Toni
  • 187
  • 5
  • 15

1 Answers1

8

I suspect the answer to this is to be had in an undecorated button. Set the white image as the icon and the yellow image as the roll-over icon. Like this:

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

class HoverImage {

    HoverImage(Image img1, Image img2) {
        JButton b = new JButton(new ImageIcon(img1));
        b.setRolloverIcon(new ImageIcon(img2));

        b.setBorderPainted(false);
        b.setContentAreaFilled(false);

        JOptionPane.showMessageDialog(null, b);
    }

    public static void main(String[] args) throws Exception {
        URL url1 = new URL("https://i.stack.imgur.com/XZ4V5.jpg");
        URL url2 = new URL("https://i.stack.imgur.com/7bI1Y.jpg");
        final Image img1 = ImageIO.read(url1);
        final Image img2 = ImageIO.read(url2);
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                new HoverImage(img1, img2);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433