 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
fb Guest
|
Posted: Sat Apr 22, 2006 6:12 am Post subject: Parsing a JList item...Or Not. :-) |
|
|
Hello again. I have a JList that is populated in the following format:
"4 - John Doe"
"2 - Bob Dole"
"6 - 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?
ttyl.
fb
--
"I've been on the wagon now for more than a decade. Not a single goto
in all that time. I just don't need them any more. I don't even use
break or continue now, except on social occasions of course. And I
don't get carried away." --Richard Heathfield |
|
| Back to top |
|
 |
Piet Guest
|
Posted: Sat Apr 22, 2006 9:12 am Post subject: Re: Parsing a JList item...Or Not. :-) |
|
|
| Quote: | Hello again. I have a JList that is populated in the following format:
How is this JList populated? I assume that you get a list of objects |
from somewhere, then extract the the User_ID and the Name as Strings,
concatenate the two strings and put the resulting string in the JList.
| Quote: | "4 - John Doe"
"2 - Bob Dole"
"6 - 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?
Use a custom cell renderer. I assume that at one point, you receive the |
User_ID and the name as separate pieces of data. Instead of combining
these two pieces and stuff the resulting string in the JList, generate
an custom data object from which you can cleanly extract the User_ID
and the name and use a ListCellRenderer to display the object in the
way you want.
Even simpler: goverride the toString() method of your custom data
object. 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();
jFrame.show();
}
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){
this.name = name;
this.id = id;
}
public String toString(){
return this.id+" - "+this.name;
}
public int getId(){
return this.id;
}
public String getName(){
return this.name;
}
}
| Quote: |
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?
Yes. The method getValueIsAdjusting() from the ListSelectionEvent tells |
you whether the actual event is only one from a chain of events. If
thats the case, you can ignore it.
| Quote: |
ttyl.
What does that mean? |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|