Hi all

We have to create a program that will require a lot of database interaction.

I would like to create a JList with all events where a user can select an event he would like to edit.
Is there a good way to store ID's into the list and retrieve them easily? Without having to put the ID into the displayed text and then fooling around with substring etc...

Thanks in advance
Greetings, Kenny

Dani AI

Generated

Building on 's hint and 's followup: both JList and JComboBox can hold arbitrary objects in their models, so the visible text need not contain the ID. A few practical patterns and cautions that help in real applications follow.

Keep the model objects tiny and immutable (just an id and display fields), update the Swing model on the Event Dispatch Thread, and run DB queries off the EDT (SwingWorker is the usual pattern). Avoid storing JDBC connections, ResultSet objects, or heavy domain graphs inside list items — store only the ID and the small fields you need for display.

Example pattern (conceptual):

public final class EventItem {
    private final long id;
    private final String label;
    public EventItem(long id, String label) { this.id = id; this.label = label; }
    public long getId() { return id; }
    public String toString() { return label; } // default renderer uses this
}

Use a typed model (DefaultListModel<EventItem> or DefaultComboBoxModel<EventItem>) and retrieve getSelectedValue() to access the id. If you need custom formatting (icons, multiple columns, timestamps), use a ListCellRenderer / ComboBoxRenderer rather than overloading toString() for display-only tweaks.

Troubleshooting and best practices:

  • Implement equals/hashCode only if you plan to compare or remove items by value.
  • For very large datasets, implement paging, incremental search, or a virtual list rather than loading everything into memory.
  • Keep ID types consistent with the DB (int vs long) and avoid using display text as a key.

For Swing details see the Oracle tutorials: How to Use Lists, How to Use Combo Boxes, and Concurrency in Swing.

Recommended Answers

All 5 Replies

You could create a little class to hold the event description and the database ID (and maybe any other relevant stuff). You can add those class members to the JList, and it will display them by calling their toString() method, so define a toString() that just returns the event description. When you query the selected item, you'll get the whole object (not just the displyed string), so you can retrieve its ID directly from it

Great, didn't know that was possible :)
Thanks, that's really a great help :)

Greets, Kenny.

Hi

I have one more question.
I know I could test this, but we're still designing, and there's little time, so if anyone who already knows could just tell me that would come in quite handy.

Is it possible to do this (what I asked before) with a dropdown list too? I bet the answer is yes, but I wanted to make sure.

Tanks in advance
Greets, Kenny

Yes

Ok, thanks :icon_wink:

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.