Hi I am trying to add an icon to my button, it seems it works fine for one image but as soon as i change the image to some other image i get this error: the only difference is that the previous image had a resolution of 48x48 and the new image has a resolution of 32x32
Here is the code:
dashboardButton = new JButton("Dashboard", new ImageIcon("images/dashbd.png"));
dashboardButton.addActionListener(this);
topPanel.add(dashboardButton, topCC.xy(2,2));
This is the error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:338)
at in.tcs.dct.ui.custom.UnifiedToolbarButtonUI.paintIcon(UnifiedToolbarButtonUI.java:48)
at javax.swing.plaf.basic.BasicButtonUI.paint(BasicButtonUI.java:216)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:793)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:121)
at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:221)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:219)
at java.awt.Dialog.show(Dialog.java:1072)
at javax.swing.JOptionPane.showOptionDialog(JOptionPane.java:871)
at javax.swing.JOptionPane.showConfirmDialog(JOptionPane.java:797)
at javax.swing.JOptionPane.showConfirmDialog(JOptionPane.java:759)
at in.tcs.dct.ui.DCTFrame.windowClosing(DCTFrame.java:146)
at java.awt.AWTEventMulticaster.windowClosing(AWTEventMulticaster.java:349)
at java.awt.Window.processWindowEvent(Window.java:2045)
at javax.swing.JFrame.processWindowEvent(JFrame.java:296)
at java.awt.Window.processEvent(Window.java:2003)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
..as soon as i change the image to some other image i get this error: the only difference is that the previous image had a resolution of 48x48 and the new image has a resolution of 32x32
So you are saying this code fails for you? It works just fine here.
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ChangingIconResolution {
public static void main(String[] args) throws Exception {
final BufferedImage img32 = new BufferedImage(
32, 32, BufferedImage.TYPE_INT_RGB);
final BufferedImage img48 = new BufferedImage(
48, 48, BufferedImage.TYPE_INT_RGB);
Runnable r = new Runnable() {
@Override
public void run() {
final JButton btn = new JButton(new ImageIcon(img48));
ActionListener animationAction = new ActionListener() {
int index = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (index++%3==0) {
btn.setIcon(new ImageIcon(img32));
} else {
btn.setIcon(new ImageIcon(img48));
}
}
};
Timer timer = new Timer(1000, animationAction);
timer.start();
JOptionPane.showMessageDialog(null, btn);
timer.stop();
}
};
SwingUtilities.invokeLater(r);
}
}
There was an issue with the image. I had actually created an icon from the image using an online editor that gave me an *.ico file, which I had converted to *.png. Hence got the error. When I used another image editor that gave me an output image in *.jpg/*.png format it worked. And also check your the path is correct. I got an error when I set the icon to a Frame (Task bar). So check that your image's path is correct.
So you are saying this code fails for you? It works just fine here. - said "Andrew Thompson"
So it is a error in path.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.