Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-26-2009, 08:11 PM
jon80's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 195
Rep Power: 3
jon80 is on a distinguished road
Default [SOLVED] [newbie] java.lang.String cannot be cast to javax.swing.Icon
Any idea what the problem is here?




Code:
ToolBarFrame.java
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
		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"))
			{
				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 {
		
		public ColorAction (String name, ImageIcon icon, Color c)
		{
			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;
}


Test.java
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);
			}
		});

	}

}
Output:
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)
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-26-2009, 08:20 PM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-26-2009, 08:51 PM
jon80's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 195
Rep Power: 3
jon80 is on a distinguished road
Default
Well I think I'm passing an ImageIcon:
Code:
Action blueAction = new ColorAction("Blue", new ImageIcon("C:\\icons\\blue-ball.png"), Color.BLUE);

...

public ColorAction (String name, ImageIcon icon, Color c)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-26-2009, 08:59 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Code:
putValue(Action.SMALL_ICON, name);
is better
Code:
putValue(Action.SMALL_ICON, icon);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-26-2009, 09:10 PM
jon80's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 195
Rep Power: 3
jon80 is on a distinguished road
Default
Did you mean...?

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");
It still loads the error which starts at:
Code:
...
bar.add(blueAction);
...
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-26-2009, 09:17 PM
jon80's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 195
Rep Power: 3
jon80 is on a distinguished road
Default
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-27-2009, 02:17 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
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 —
Code:
C:\\icons\\blue-ball.png
the double back slashes are for ms only. Use a single forward slash for java/all platforms.
You can move the icons folder into your project directory and use the path
Code:
icons/blue-ball.png
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String) baltimore New To Java 2 09-18-2008 08:30 AM
java.lang.NoClassDefFoundError: javax/swing/GroupLayout$Group scotter59 NetBeans 6 07-10-2008 08:28 PM
Cast Error Caught (change) Class is really: java.lang.String barney Advanced Java 1 08-02-2007 05:07 PM
map javax.swing.text.Element to javax.swing.text.View elizabeth New To Java 1 07-30-2007 08:02 PM
java.lang.NoClassDefFoundError: javax/activation/DataSource bbq Advanced Java 1 07-05-2007 05:26 AM


All times are GMT +2. The time now is 11:54 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org