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:
Code :
//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:
Code :
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.
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).
Re: ClassCastException with Enum Combo Box
Sorry, you're right, the error was in another part of the code.
Re: ClassCastException with Enum Combo Box
Quote:
Originally Posted by
Diplo
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.
Re: ClassCastException with Enum Combo Box
Quote:
Originally Posted by
KevinWorkman
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.