> I have a JList with a DefaultListModel, and I add a ListSelectionListener to
> my JList (code below). However, when I click on an item in the Jlist, it
> doesn't alter the selected state of an item. Since valueChanged() is from an
> interface, I cannot call super on it. Can someone please point out how I can
> make my ListSelectionListener reflect the selected/deselected state of items
> in it when I click upon them? Thanks, Ike
Are you suggesting that adding any selection listener to a JList stops
selection changing? Why not try a little test.
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
class SelectionTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { public void run() {
swing();
}});
}
private static void swing() {
JFrame frame = new JFrame("SelectionTest");
frame.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE
);
JList list = new JList(new Object[] {
"Fred", "Jim", "Sheila"
});
list.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
//throw new Error();
}
}
);
frame.add(list);
frame.pack();
frame.setVisible(true);
}
}
Works for me. Although things go a little astray if it throws an error.
I do get copious notification in the console, however. And it repaints
differently if I move a window over it.
> DefaultListModel listmodel_1=new DefaultListModel();
Not a great name.
> JList nameDisplay=new JList(listmodel_1);
> nameDisplay.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
[quoted text clipped - 4 lines]
> private class nameDisplayListSelectionListener implements
> ListSelectionListener{
Another less than inspiring name.
> public void valueChanged(ListSelectionEvent e){
> if(ignoreListListener){
> return;
> }
> ignoreListListener=true;
If you're going to do this, do make sure you reset the flag within a
finally block. If your listener throws an exception you are going to be
in a state.
> setCursor(new Cursor(Cursor.WAIT_CURSOR));
You shouldn't be blocking the Event Dispatch Thread (EDT), so this
shouldn't be necessary.
> JList j = (JList)(e.getSource());
Nice name.
> ListSelectionModel lsm = j.getSelectionModel();
It's perhaps not great to have to decode acronyms. It's the selectionModel.
> j.repaint();
Utterly pointless, unless you are planning on throwing an exception.
> for(int i=0;i<qplayers.size();i++){
> Qplayer qp = (Qplayer)qplayers.get(i);
> if(qp.inlist){
Public variables?
> if(!qp.available && lsm.isSelectedIndex(i)){
> qp.available=true;
> lastauditstring=auditstring;
Have we set auditstring to anything nice?
> auditstring=qp.nick+" 1";
> if(!lastauditstring.equals(auditstring))
[quoted text clipped - 17 lines]
> if(whoIsLosingIt!=null)
> whoIsLosingIt.grabFocus();
Huh?
> ignoreListListener=false;
>
> }
> }
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/