开发者

Element inserted into custom ListModel is automatically selected if between two selected values

I've noticed is that when an element is added to a JList and the element happens to fall within a range of values that are selected, it ends up being selected by default. In fact, if an element is added just above a selected value i开发者_运维技巧t is added to the selection.

I've tried looking at the JList code (in the open JDK6) and the BasicListUI code, but I'm having trouble teasing out the details of why this happens or what I need to do to fix it.

I'm considering supplying a custom SelectionModel, as that would be a sensible place to do some of the other work in my application, but I'm afraid that might make the problem worse, or harder to fix. Does anyone know why this happens? What I can do to fix it?

I've created this example that demonstrates the issue:

package jlistsscce;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class JListSSCCE extends JPanel
{
    private final JList list;
    private ScheduledExecutorService ses;

    public JListSSCCE()
    {
        list = new JList();
        list.setModel(new DefaultListModel());
        ses = Executors.newSingleThreadScheduledExecutor();
        ses.scheduleAtFixedRate(new NewElement(), 100, 100, TimeUnit.MILLISECONDS);
        add(list);
    }

    private static void createGui()
    {
        // create new JFrame
        JFrame jf = new JFrame("JListSSCCE");

        // this allows program to exit
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // You add things to the contentPane in a JFrame
        jf.getContentPane().add(new JListSSCCE());

        // size frame
        jf.pack();

        // make frame visible
        jf.setVisible(true);

    }

    public static void main(String[] args)
    {
        // threadsafe way to create a Swing GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run()
                {
                    createGui();
                }
            }
        );
    }

    private class NewElement implements Runnable
    {
        int n = 0;
        @Override
        public void run()
        {
            ((DefaultListModel)list.getModel()).add((int)Math.floor(Math.sqrt(n)), ("hey"+n));
            n++;
        }

    }
}


This isn't the problem but I believe you should be using a Swing Timer instead of an Executor so the code get executed on the EDT.

Yes the problem is the selection model. The last time I looked at the code I found it rather confusing, so I'm not sure you want to play with it.

The only thing I can think of is to make sure you are using the multiple selection interval. Then after inserting an elemnt you check to see if the element is selected. If it is then you remove the selection for that element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜