Results 1 to 7 of 7
- 05-26-2009, 07:11 PM #1
[SOLVED] [newbie] java.lang.String cannot be cast to javax.swing.Icon
Any idea what the problem is here?
:confused:
Output:Java Code:[B]ToolBarFrame.java[/B] package homenetwork.bkr.training; import javax.swing.*; import java.awt.*; import java.awt.event.*; @SuppressWarnings("serial") public class ToolBarFrame extends JFrame { public ToolBarFrame() { setTitle("Toolbar test"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); //add a panel for color change panel = new JPanel(); add(panel, BorderLayout.CENTER); //set up actions [B]Action blueAction = new ColorAction("Blue", new ImageIcon("C:\\icons\\blue-ball.png"), Color.BLUE); Action yellowAction = new ColorAction("Yellow", new ImageIcon("C:\\icons\\yellow-ball.png"), Color.YELLOW); Action redAction = new ColorAction("Red", new ImageIcon("C:\\icons\\red-ball.png"), Color.RED); Action exitAction = new AbstractAction("Exit", new ImageIcon("C:\\icons\\exit.png"))[/B] { public void actionPerformed(ActionEvent event) { System.exit(0); } }; exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit"); //populate tool bar JToolBar bar = new JToolBar(); bar.add(blueAction); bar.add(yellowAction); bar.add(redAction); bar.addSeparator(); bar.add(exitAction); add(bar, BorderLayout.NORTH); //populate menu JMenu menu = new JMenu("Color"); menu.add(yellowAction); menu.add(blueAction); menu.add(redAction); menu.add(exitAction); JMenuBar menuBar = new JMenuBar(); menuBar.add(menu); setJMenuBar(menuBar); } public class ColorAction extends AbstractAction { [B]public ColorAction (String name, ImageIcon icon, Color c)[/B] { putValue(Action.NAME, name); putValue(Action.SMALL_ICON, name); putValue(Action.SHORT_DESCRIPTION, name); putValue("Color", c); } public void actionPerformed(ActionEvent event) { Color c = (Color) getValue("Color"); panel.setBackground(c); } } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; private JPanel panel; } [B]Test.java[/B] package homenetwork.bkr.training; import java.awt.*; import javax.swing.JFrame; public class Test { /** * @param args */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { ToolBarFrame frame = new ToolBarFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } }
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to javax.swing.Icon
at javax.swing.AbstractButton.setIconFromAction(Unkno wn Source)
at javax.swing.AbstractButton.configurePropertiesFrom Action(Unknown Source)
at javax.swing.AbstractButton.setAction(Unknown Source)
at javax.swing.JToolBar.add(Unknown Source)
at homenetwork.bkr.training.ToolBarFrame.<init>(ToolB arFrame.java:37)
at homenetwork.bkr.training.Test$1.run(Test.java:16)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 05-26-2009, 07:20 PM #2
Yes, a String cannot be cast to an Icon. I think you probably meant to use icon somewhere in your action's constructor.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-26-2009, 07:51 PM #3
Well I think I'm passing an ImageIcon:
Java Code:Action blueAction = new ColorAction("Blue", new ImageIcon("C:\\icons\\blue-ball.png"), Color.BLUE); ... public ColorAction (String name, ImageIcon icon, Color c)
- 05-26-2009, 07:59 PM #4
is betterJava Code:putValue(Action.SMALL_ICON, name);
Java Code:putValue(Action.SMALL_ICON, icon);
- 05-26-2009, 08:10 PM #5
Did you mean...?
It still loads the error which starts at:Java Code:Action blueAction = new ColorAction("Blue", new ImageIcon("C:\\icons\\blue-ball.png"), Color.BLUE); blueAction.putValue(Action.SHORT_DESCRIPTION, "Blue"); Action yellowAction = new ColorAction("Yellow", new ImageIcon("C:\\icons\\yellow-ball.png"), Color.YELLOW); yellowAction.putValue(Action.SHORT_DESCRIPTION, "Yellow"); Action redAction = new ColorAction("Red", new ImageIcon("C:\\icons\\red-ball.png"), Color.RED); redAction.putValue(Action.SHORT_DESCRIPTION, "Red"); Action exitAction = new AbstractAction("Exit", new ImageIcon("C:\\icons\\exit.png")) { public void actionPerformed(ActionEvent event) { System.exit(0); } }; exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit");
Java Code:... bar.add(blueAction); ...
- 05-26-2009, 08:17 PM #6
Umm...Sorry I didn't get it straight away, thanks for spotting that :)
Any idea how to make the path to the image relative to the application itself when it is compiled.
In the meantime I would like to have the images stored in the project directory of the application itself?
- 05-27-2009, 01:17 AM #7
Any idea how to make the path to the image relative to the application itself when it is compiled
Yes. See How to Use Icons for a discussion of this subject and especially the section Loading Images Using getResource.
Using ImageIcon is an older method of loading images. One drawback of using it is that it doesn't give any feedback if it could not find the file, could not recognize the image format, of if the image data is corrupted or unreadable.
The newer way to load images is shown here Reading/Loading an Image.
In the meantime I would like to have the images stored in the project directory of the application itself
Okay.
About the image path in your code —
the double back slashes are for ms only. Use a single forward slash for java/all platforms.Java Code:C:\\icons\\blue-ball.png
You can move the icons folder into your project directory and use the path
Java Code:icons/blue-ball.png
Similar Threads
-
java.lang.NoClassDefFoundError: javax/activation/DataSource
By bbq in forum Advanced JavaReplies: 2Last Post: 07-21-2012, 01:15 AM -
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String)
By baltimore in forum New To JavaReplies: 2Last Post: 09-18-2008, 07:30 AM -
java.lang.NoClassDefFoundError: javax/swing/GroupLayout$Group
By scotter59 in forum NetBeansReplies: 6Last Post: 07-10-2008, 07:28 PM -
Cast Error Caught (change) Class is really: java.lang.String
By barney in forum Advanced JavaReplies: 1Last Post: 08-02-2007, 04:07 PM -
map javax.swing.text.Element to javax.swing.text.View
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 07:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks