Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: ClassCastException with Enum Combo Box

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default ClassCastException with Enum Combo Box

    **Never mind, there was an error somewhere else in the program.**

    If anyone is interested though:

    Original problem was a ClassCastException error I was getting. Relevant code is:

    	//Menu drop box items. 
    	public enum shapes {Circle, Square, Oval,}
    	//ComboBox
    	private JPanel shapesPanel = new JPanel();
    	private JComboBox shapesComboBox = new JComboBox(shapes.values());
     
    //...More code...
     
    	protected void shapesPanelMouseClicked(MouseEvent evt) {
     
    		//Get x and y values of mouse location
    		//and selected shape.
    		int x = evt.getX();
    		int y = evt.getY();
    		shapes shapeChoice = (shapes) shapesComboBox.getSelectedItem();
     
    		switch (shapeChoice){
     
    		case Circle:
    			drawCircle(x, y); break;
    		case Square:
    			drawSquare(x, y); break;
    		case Oval:
    			drawOval(x, y); break;			
     
    		}

    All I'd done is left a line similar to:

     	String shapeChoice = shapesComboBox.getSelectedItem();

    elsewhere in the code. This was used before I changed from using a String Array to an enum for the JComboBox.
    Last edited by Diplo; July 26th, 2011 at 01:23 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ClassCastException with Enum Combo Box

    Hmm, I just did a quick test with my own enum and JComboBox, and it seems to work fine. Would you mind putting together an SSCCE (that's not all your code, just the relevant parts- you don't even need to have a GUI, just print out the default selected item of an invisible JComboBox initialized with an enum).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Diplo (July 26th, 2011)

  4. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException with Enum Combo Box

    Sorry, you're right, the error was in another part of the code.

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ClassCastException with Enum Combo Box

    Quote Originally Posted by Diplo View Post
    Sorry, you're right, the error was in another part of the code.
    That's fine. You might want to keep your original thread so that future users know what's going on when they search the forums. You might even write up a post showing the error, so that people can learn from it. That's pretty much the point of these forums.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Diplo (July 26th, 2011)

  7. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException with Enum Combo Box

    Quote Originally Posted by KevinWorkman View Post
    That's fine. You might want to keep your original thread so that future users know what's going on when they search the forums. You might even write up a post showing the error, so that people can learn from it. That's pretty much the point of these forums.
    Ok outlined the rough details of the error. Was a pretty stupid error on my part though so not sure how many it will help! Thanks for your help anyway.

Similar Threads

  1. [SOLVED] Enum problem
    By pajfilms in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2011, 07:26 AM
  2. Adding another related combo box ?
    By shad3d in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2011, 01:53 PM
  3. ClassCastException
    By kamalivy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 20th, 2010, 10:57 PM
  4. Combo/TextField Help
    By GRossi0511 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 26th, 2009, 08:15 PM
  5. Loading contents of a Database into a Combo box
    By tyolu in forum JDBC & Databases
    Replies: 3
    Last Post: June 22nd, 2009, 08:14 AM