Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2005

Tip: Looking for answers? Try searching our database.

JList Help

Thread view: 
bwilde@gmail.com - 05 Sep 2005 00:41 GMT
Hello,

I am trying something that I feel should be simple, but I am having
some trouble with it.  I want to have a list of strings, and when the
user clicks on one element, I want to print that element number to the
screen.

At this point, I made a DefaultListModel and added my strings to it.  I
then make a new JList and pass my model into it.  Everything seems to
be working up to this point.

I then call 'addListSelectionListener' on my list, and have a seperate
listener class.  In the listener class, I try to do a call to
'getSelectedIndex' on my list, but it never returns anything other than
-1, regardless of what element I'm clicking on.

I have a feeling that I'm missing a listener or something here.  Can
anybody help me out?

Thanks,
Ben
Thorsten Suckow-Homberg - 05 Sep 2005 01:05 GMT
Could you post some lines of your source along with your question? This
would be helpfull...
Thorsten Suckow-Homberg - 05 Sep 2005 01:07 GMT
i.e. the code of your ListSelectionListener.valueCanged()-method....
Andrew Thompson - 05 Sep 2005 02:01 GMT
> Could you post some lines of your source along with your question? This
> would be helpfull...

Good idea.  Some actual lines from the OP's breaking code
would get us closer to an answer.

Unfortunately, if code is breaking, it is not always obvious
which bit is broken, so the OP often posts lines of code they
*suspect* of causing the problem, when in fact it is elsewhere.

For that reason (and others) I suggest, not code snippets,
but an SSCCE[1], which the OP should be able to do in less
than 30[2] lines of code.

[1] <http://www.physci.org/codes/sscce.jsp>
[2] I'd have said 20, but give allowances for a first attempt.

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"Takes her over the breakers, to where the water is calm.."
Paul Kelly 'Deeper Water'

bwilde@gmail.com - 05 Sep 2005 03:01 GMT
Well, the code is not actually breaking.  Everything compiles just
fine.  So, hopefully, snippets will add some insight...

In my main class, I have:

DefaultListModel model = new DefaultListModel();
JList fileList = new JList(model);
fileList.setSelectedIndex(0);
fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fileList.setLayoutOrientation(JList.VERTICAL);
fileList.addListSelectionListener(new FileListListener());

I then add elements to my list with code similar to this:

for(int i = 0; i < 6; i++)
     model.add(i,"ListElement");

And my listener class looks like this:

protected class FileListListener implements ListSelectionListener
{
    public void valueChanged(ListSelectionEvent e)
    {
        if(e.getValueIsAdjusting())
            return;

        System.out.println("Index: " + fileList.getSelectedIndex());
    }
}

So, everything builds just fine and the GUI looks great.  Every time I
click on a list element, it prints Index: -1.  This tells me that the
listener is set up fine, but that something is not in place so my JList
knows what is selected.

Does this help?

Thanks,
Ben
Andrew Thompson - 05 Sep 2005 03:10 GMT
> Well, the code is not actually breaking.  

Yes it is.

>..Everything compiles just fine.

Yes, but my definition of 'break' is.. written in the
document to which I linked.  Did you read it?

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"I talk of freedom, you talk of the flag. I talk of revolution, you'd much
rather brag.."
Live 'White Discussion'

Michael Dunn - 05 Sep 2005 03:20 GMT
you will get the described behaviour if you have this line twice
JList fileList = new JList(model);

1) as a class member
2) inside the constructor

remove (2) and it should be OK
bwilde@gmail.com - 05 Sep 2005 03:23 GMT
Ah beautiful.  Thank you very much... that was exactly my problem.
Thanks again!
Thomas Hawtin - 05 Sep 2005 15:28 GMT
> Ah beautiful.  Thank you very much... that was exactly my problem.
> Thanks again!

Pity it was only indirectly implied by the code eventually given.

Tom Hawtin
Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.