Reshad
Reshad

Reputation: 2652

how do I show all enum values from database in Java

I have 3 values stored as ENUM in my MySQL database.

What I basically want to do is retrieve all this three items and store them in a JComboBox. with the selected enum item from the database as the selected item in the combobox.

At this moment I retrieve just only the current value as string from database and use this method to put all items in the combo box.

private enum statusTypes {Beschikbaar, verhuurd, onderhoud};
txtstatus = new JComboBox();
txtstatus.setModel(new DefaultComboBoxModel(statusTypes.values()));

The way to get the item from the database is like

String s = model.getStatus();

So how can I tell Java to put the value I get as the first value of my combo box?

Upvotes: 0

Views: 1105

Answers (1)

user800014
user800014

Reputation:

You need to convert your String to the Enum:

//consider using Java naming convention
private enum StatusTypes {BESCHIKBAAR, VERHUURD, ONDERHOUD};
...
String s = model.getStatus();
...
StatusTypes status = StatusTypes.valueOf(s);

Upvotes: 2

Related Questions