0

My select query returns 2 columns, ID and Text I want to display the value of Text in an Jlist which works :)

When a user clicks on the specific Jlist item, this item needs to be deleted from the database. To do this safely I need to know the ID.

How do I get to know the ID of that specific item of my JList? Can I store this with the Jlist somehow?

2 Answers 2

1

Well, since you can define the type of the elements in a JList just create a class that contains the id and text and then query the selected element for its id. Shouldn't be too hard.

Note that if you don't want to display the id in the list (or maybe display it in a non-standard way), you'd either have to override the element's toString() method or provide a custom ListCellRenderer.

Sign up to request clarification or add additional context in comments.

Comments

0

JList is an UI component following a Model-View pattern.

I suggest you to build a ListModel based on your query results, populate it with the information you need, and then build an ActionListener reacting on events on your JList. This ActionListener would just read the selected index on your JList, and that brings you to your original object with all the information you stored on the model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.