Results 1 to 7 of 7
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] [newbie] java.lang.String cannot be cast to javax.swing.Icon

    Any idea what the problem is here?

    :confused:


    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);
    			}
    		});
    
    	}
    
    }
    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)

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    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

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    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)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    putValue(Action.SMALL_ICON, name);
    is better
    Java Code:
    putValue(Action.SMALL_ICON, icon);

  5. #5
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Did you mean...?

    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");
    It still loads the error which starts at:
    Java Code:
    ...
    bar.add(blueAction);
    ...

  6. #6
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    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?

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    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
    Java 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
    Java Code:
    icons/blue-ball.png

Similar Threads

  1. Replies: 2
    Last Post: 07-21-2012, 02:15 AM
  2. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  3. Replies: 6
    Last Post: 07-10-2008, 08:28 PM
  4. Replies: 1
    Last Post: 08-02-2007, 05:07 PM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 08:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •