Parsing a JList item...Or Not. :-)
08/11/2018 12:08
Hello again. I have a JList that is populated in the following format:
4 - John Doe2 - Bob Dole6 - Me You etc.

The number is a user ID tag that aligns with a database User_ID field. I want to determine the user_id of the person from the list using the ID number in the String.

I was thinking of parsing the String, but that seems like a lot of work to get one number...The last time I had to parse something was in a compiler construction class 10 years ago. Terrible memories from that class...I'd rather not develop a parser. Is there another way?
class GetListItemListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { JList theList = (JList) e.getSource(); String theUserItem = (String) theList.getSelectedValue();
//Parse string or...? System.out.println(theUserItem); }}
On a seperate note: I've noticed that if I click on a JList item with the mouse, it seems to fire the ListSelectionListener twice. Once for a down mouseclick and once for a release mouseclick. Is there a way to change this?


Source is Usenet:
Sign in to add a comment

Answer score: 5
08/11/2018 12:08 - How is this JList populated? I assume that you get a list of objectsfrom somewhere, then extract the the User_ID and the Name as Strings,concatenate the two strings and put the resulting string in the JList.

Use a custom cell renderer. I assume that at one point, you receive theUser_ID and the name as separate pieces of data. Instead of combiningthese two pieces and stuff the resulting string in the JList, generatean custom data object from which you can cleanly extract the User_IDand the name and use a ListCellRenderer to display the object in theway you want.

Even simpler: goverride the toString() method of your custom dataobject. Here is an example:

import java.awt.*;import java.awt.event.*;import javax.swing.event.*;import javax.swing.*;
public class JListDemo implements ListSelectionListener{ JFrame jFrame; JList jList; public JListDemo(){ jFrame = new JFrame(List Demo); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultListModel dlm = new DefaultListModel(); dlm.addElement(new ListDataObject(Person Number 1,1)); dlm.addElement(new ListDataObject(Person Number 2,2)); dlm.addElement(new ListDataObject(Person Number 3,3)); jList = new JList(dlm); jList.addListSelectionListener(this); jFrame.getContentPane().add(new JScrollPane(jList)); jFrame.pack();; } public static void main(String[] args){ JListDemo jld = new JListDemo(); } public void valueChanged(ListSelectionEvent lse){ if (!lse.getValueIsAdjusting()){ System.out.println(User Name: +((ListDataObject)jList.getSelectedValue()).getName()); System.out.println(User ID: +((ListDataObject)jList.getSelectedValue()).getId()); } }}class ListDataObject{ private int id; private String name; public ListDataObject(String name,int id){ = name; = id; } public String toString(){ return -; } public int getId(){ return; } public String getName(){ return; }}
Yes. The method getValueIsAdjusting() from the ListSelectionEvent tellsyou whether the actual event is only one from a chain of events. Ifthats the case, you can ignore it.

What does that mean?

Source is Usenet:
Sign in to add a comment

Helpforce eDiscover provides technical articles updated each dayHelpforce eDiscover RSS feed contains the latest technical articles in RSS
Click the logo to go back to the main page
Search eDiscover

Click an icon to go to that category

Helpforce eDiscover contains articles about Microsoft Windows Helpforce eDiscover contains articles about Apple products and MacOS Helpforce eDiscover contains articles about Linux and POSIX operating systems Helpforce eDiscover contains articles about Helpforce Helpforce has a large variety of technical information and articles for you to read Helpforce eDiscover contains articles about databases, MYSQL, SQL Server Oracle Helpforce eDiscover contains articles about Java, JVM and the JRE Helpforce eDiscover contains articles about the QNX operating system Helpforce eDiscover contains articles about Oracle Solaris and Open Solaris Helpforce eDiscover contains articles about RISC OS, Acorn and the BBC Micro Helpforce eDiscover contains articles about Amiga and AmigaOS

Type your comment into the box below