Jlist problem

M

Mika Suomi

I have a JList in a window and I put item there from String[] lst-table.
When I click item in a list it gives result twice.
Why?

private JList list ;

list=new JList(lst);

public void valueChanged(ListSelectionEvent e) {

System.out.println(list.getSelectedIndex());

}
 
J

Jan Liße

maybe it works when you set this:

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
M

Mika Suomi

Little more info about problem if I choose for exsample item 3 when I click
mouse the first return comes and when I release mouse the second release
comes.

Mika Suomi
 
J

Jan Liße

hi mika,now i understand your problem! try this snippet:public void
valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) //true if user still manipulates
selection { if (list.getSelectedIndex() == -1) {
System.out.println(list.getSelectedIndex());

}
}
 
J

Jan Liße

again, here correctly formatted:

public void valueChanged(ListSelectionEvent e)
{
if (e.getValueIsAdjusting == false)
{
if (list.getSelectedIndex() != -1)
{
// no selection, do something
}
System.out.println(list.getSelectedIndex());
}

greetz,
jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top