Good Afternoon all,

I'm working on a project for school, and we have a part where we want a combobox filled with data from a mysql database, tabel = sport.

We can work out how to fill a normal combobox just with strings, but we can't find out how to link it to our mysqldatabase.

If anyone could help we would be very pleased.

btw, we can make the connection to our database and put data in, and we're almost there with getting it on our screen.

Thanks in advance, GeKKeR as member of the projectgroup I-6

Recommended Answers

All 15 Replies

JComboBox is very flexible in that you can put any object you like in the model. You don't mention what you mean exactly by "link it to the database", but if you need to do something like get an ID for a selected entry, a small TypeEntry object can be used

class TypeEntry{
    private int value;
    private String label;

    public TypeEntry(int id, String label){
        this.value = id;
        this.label = label;
    }
    public int getValue(){
        return value;
    }
    public String toString(){
        return label;
    }
}

and added to the combo box model like so

rs = stmt.executeQuery("SELECT Id,Label FROM SomeTable ORDER BY Id");
while (rs.next()){
    comboBox.addItem(new TypeEntry(rs.getInt(1),rs.getString(2).trim()));
}
rs.close();

The toString() value will be shown in the combo box and any other data you need from the selected entry can be obtained from the object returned by getSelectedItem().

Standard Swing components have no database coupling, which is as it should be (you should not mix database code with user interface code).

Get the data from the database, and use that data to fill your user interface.

Hi Ezzaral,

Can you please explain how you get the Integer of the selected item?
Yes I can return the selected string by

combo.getSelectedItem();

but how do I get it's int returned by the getValue() method?

Thanks in advance,
Cleo123

The returned object will be a TypeEntry object. You have to cast to that and use its getValue() method.

TypeEntry selectedType = (TypeEntry)combo.getSelectedItem();
int typeValue = selectedType.getValue();

No problem, I fixed the problem!

TypeEntry typeentry = (TypeEntry)cb.getSelectedItem();
Integer Title = typeentry.getValue();

Thanks for the previously posted support :)

The returned object will be a TypeEntry object. You have to cast to that and use its getValue() method.

TypeEntry selectedType = (TypeEntry)combo.getSelectedItem();
int typeValue = selectedType.getValue();

Thanks for the response. Do you also know how you would go about showing only the ID? Since I've used Dates and Years as the ID and I would like to show the list of dates (1,2,3, etc.) and years (2008,2007, etc) in the combo box.

Cleo123

The combo box will display whatever value your toString() method returns, so just alter that method to return any representation you want.

The only purpose in making the TypeEntry structure at all was to associate some ID (or other) value with the label you want to present to the user.

The combo box will display whatever value your toString() method returns, so just alter that method to return any representation you want.

The only purpose in making the TypeEntry structure at all was to associate some ID (or other) value with the label you want to present to the user.

The problem is that I want to show the INT, not a string, so I changed the TypeEntry to this:

class ComboBoxItemID {
   private int number;

  public ComboBoxItemID(int numberID) {
  this.number = numberID;
   }

    public int getValue2() {
     return number;
  }
}

Following the same structure as before, but without the String value. When I system print the values selected I get the correct numbers (date and year) but the date and year combo boxes contain the name of my java file followed by $...

If you just want the int to show, you don't even need the TypeValue class. Just put Integer objects in the combo box model and cast them back to Integer when you getSelectedItem().

The only reason to make a small custom class and override toString() is if you want to display some specific label to your users, but store some other data from their selection.

Ezzaral hello.

I did everything you said about the JComboBox and works perfectly.
But how I get my JComboBox is selected with the id (so the value)?

for example

cb.setSelectedItem (id)

Ezzaral hello.

I did everything you said about the JComboBox and works perfectly.
But how I get my JComboBox is selected with the id (so the value)?

for example

cb.setSelectedItem (id)

i cannot have the combo box

Using this code the itemlistener is not properly working...
when using the combo without focusing .. It's not generating the corresponding type entry value...rather it gives an error saying "cannot able to convert string to TypeEntry object"

The above mentioned code does work perfectly fine and I am really grateful but there is one problem I am facing at the moment that is about how to set the selelected item while retrieving the records from the database??

DaniWeb Member Rules include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question

This thread is closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.