Setting multiple icons in JButton

I have a JButton on which I installed a custom icon. Now I want it to display another icon on top of the one that is already displayed when I click on it with the mouse, but I can’t figure out how to do it, because if I use button.setIcon(icon); , it will replace the icon that is already displayed. How do I make this as simple as possible?

+4
source share
5 answers

One way to do this:

Create the icon that you want to see when the pointer hovers over the button using some image editing tool. And set this image after the mousehover event.

ps use any pic editing tool and you can easily create an overlay image.

I also saw that in the AbsractButton class there is the concept of flipping an icon. You can also use this.

0
source

I have a JButton on which I installed a custom icon. Now I want to display another icon on the panel, which is already displayed when I drag the mouse over it, but I can’t figure out how to do it because if I use button.setIcon (icon); it will replace the icon that is already displayed. How would I make it as easy as possible

  • I think about JButton.setRolloverIcon(myIcon);

JButton implemented these methods in the API

 JButton.setIcon(myIcon); JButton.setRolloverIcon(myIcon); JButton.setPressedIcon(myIcon); JButton.setDisabledIcon(myIcon); 
+4
source

If your icons are already transparent, you can easily implement your own Icon to combine the two -

 public class CombineIcon implements Icon { private Icon top; private Icon bottom; public CombineIcon(Icon top, Icon bottom) { this.top = top; this.bottom = bottom; } public int getIconHeight() { return Math.max(top.getIconHeight(), bottom.getIconHeight()); } public int getIconWidth() { return Math.max(top.getIconWidth(), bottom.getIconWidth()); } public void paintIcon(Component c, Graphics g, int x, int y) { bottom.paintIcon(c, g, x, y); top.paintIcon(c, g, x, y); } } 

You use setRolloverIcon(icon) to specify the icon you want to show when the mouse is over a button.

+3
source

Create a second version of the icon for this button that contains the overlay. Hover over the overlay image.

Another approach could be to combine the icon with its overlay on the new icon in memory and place it as an icon on the button. This can be a good approach if your icons change frequently. If this is not the case, I would definitely use the first approach.

+1
source

I find it pretty simple.

 import java.awt.*; import java.awt.image.BufferedImage; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.*; class CombinedIconButton { public static BufferedImage getCombinedImage(BufferedImage i1, BufferedImage i2) { if (i1.getHeight() != i2.getHeight() || i1.getWidth() != i2.getWidth()) { throw new IllegalArgumentException("Images are not the same size!"); } BufferedImage bi = new BufferedImage( i1.getHeight(), i1.getWidth(), BufferedImage.TYPE_INT_ARGB); Graphics g = bi.getGraphics(); g.drawImage(i1,0,0,null); g.drawImage(i2,0,0,null); g.dispose(); return bi; } public static void main(String[] args) throws Exception { URL url1 = new URL("http://i.stack.imgur.com/gJmeJ.png"); // blue circle URL url2 = new URL("http://i.stack.imgur.com/5v2TX.png"); // red triangle final BufferedImage bi1 = ImageIO.read(url1); final BufferedImage bi2 = ImageIO.read(url2); final BufferedImage biC = getCombinedImage(bi1,bi2); Runnable r = new Runnable() { @Override public void run() { JPanel gui = new JPanel(new BorderLayout()); JToggleButton b = new JToggleButton(); b.setIcon(new ImageIcon(bi1)); b.setRolloverIcon(new ImageIcon(biC)); b.setSelectedIcon(new ImageIcon(bi2)); gui.add(b); JOptionPane.showMessageDialog(null, gui); } }; // Swing GUIs should be created and updated on the EDT // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html SwingUtilities.invokeLater(r); } } 

Images taken from this answer .

+1
source

All Articles