Results 1 to 6 of 6
  1. #1
    merendo is offline Member
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    2
    Rep Power
    0

    Default ActionListener for each element in a JList

    Hi folks,

    first things first, hello to everyone as I'm new to these forums.

    Here's my question. I have a nice and neat JList with a DefaultListModel, which I can add a few elements to. Now to handle actions on that JList, I'd prefer not responding to actions performed on the JList itself, but rather to actions on each element. Is there a way to do this? I understand I can parse any sort of Object to DefaultListModel.addElement(), not sure how to implement the ActionListener though.

    The reason I want to solve my problem like this is because the elements in the JList have previously been fetched from a MySQL database. And I'd like to be able to store the ID of each row in the MySQL table to the corresponding element in the JList and simply have it when I need it, rather than having to obtain the ID through an additional query when any item in the JList is clicked.

    Thanks for your help!
    merendo

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: ActionListener for each element in a JList

    No, you can't give each element an "ActionListener" and moreover you don't need to.

  3. #3
    merendo is offline Member
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    2
    Rep Power
    0

    Default Re: ActionListener for each element in a JList

    Quote Originally Posted by Fubarable View Post
    No, you can't give each element an "ActionListener" and moreover you don't need to.
    Could you elaborate, please? Like I said, I'd like to be able to assign each element an ID (invisible to the front-end user), that is, the ID of the corresponding element in the MySQL database. Can you point me to an elegant solution how to achieve this?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: ActionListener for each element in a JList

    assign each element an ID
    If the "elements" extend JComponent class, you could use the ClientProperties to save any data you want to associate with that component.
    Or you could add the data you want associated with your object to your class itself.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: ActionListener for each element in a JList

    Do you have a class that represents the MySQL data you are displaying?
    If so then have a ListModel that holds this data, displaying whatever specific bit you are currently displaying...that data will have your MySQL id in it.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: ActionListener for each element in a JList

    I'd like to be able to assign each element an ID (invisible to the front-end user),
    Add a custom object to the ListModel that contains both the id and the data as is demonstrated by my answer in this posting: Populating databasefields in combobox. The example uses a combo box, but the concept is the same.

Similar Threads

  1. Replies: 4
    Last Post: 01-10-2012, 02:05 PM
  2. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 08:10 PM
  3. Link one JList to another JList
    By mib1bee in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2010, 07:35 PM
  4. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •