开发者

How to enable drag-and-drop inside JList

I have a JList and want the user to be able to reorder the elements in the list using drag-n-drop (using my own ListModel and ListCellRenderer, if that makes any difference).开发者_如何学C Which Objects do I need to create, and how do I process the action?


Modified Jan Taccis answer:

public class DndTest extends JFrame {

    JList<String> myList;
    DefaultListModel<String> myListModel;

    public DndTest() {
        myListModel = createStringListModel();
        myList = new JList<String>(myListModel);
        MyMouseAdaptor myMouseAdaptor = new MyMouseAdaptor();
        myList.addMouseListener(myMouseAdaptor);
        myList.addMouseMotionListener(myMouseAdaptor);

        JPanel content = new JPanel();
        content.add(myList);
        this.add(content);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    private class MyMouseAdaptor extends MouseInputAdapter {
        private boolean mouseDragging = false;
        private int dragSourceIndex;

        @Override
        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                dragSourceIndex = myList.getSelectedIndex();
                mouseDragging = true;
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            mouseDragging = false;
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (mouseDragging) {
                int currentIndex = myList.locationToIndex(e.getPoint());
                if (currentIndex != dragSourceIndex) {
                    int dragTargetIndex = myList.getSelectedIndex();
                    String dragElement = myListModel.get(dragSourceIndex);
                    myListModel.remove(dragSourceIndex);
                    myListModel.add(dragTargetIndex, dragElement);
                    dragSourceIndex = currentIndex;
                }
            }
        }
    }

    private DefaultListModel<String> createStringListModel() {
        final String[] listElements = new String[] { "Cat", "Dog", "Cow", "Horse", "Pig", "Monkey" };
        DefaultListModel<String> listModel = new DefaultListModel<String>();
        for (String element : listElements) {
            listModel.addElement(element);
        }
        return listModel;
    }

    public static void main(String[] args) {
        new DndTest();
    }
}


How about something like this:

DefaultListModel<String> myListModel = createStringListModel();
JList<String> myList = new JList<String>(myListModel);
MyMouseAdaptor myMouseAdaptor = new MyMouseAdaptor();
myList.addMouseListener(myMouseAdaptor);
myList.addMouseMotionListener(myMouseAdaptor);

private class MyMouseAdaptor extends MouseInputAdapter {
    private boolean mouseDragging = false;
    private int dragSourceIndex;

    @Override
    public void mousePressed(MouseEvent e) {
        dragSourceIndex = myList.getSelectedIndex();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (mouseDragging) {        
            int dragTargetIndex = myList.getSelectedIndex();
            String dragElement = myListModel.get(dragSourceIndex);
            myListModel.remove(dragSourceIndex);
            myListModel.add(dragTargetIndex, dragElement);
        }
        mouseDragging = false;
    }

    @Override
    public void mouseDragged(MouseEvent e) {            
        mouseDragging = true;
    }
}

private DefaultListModel<String> createStringListModel() {
    final String[] listElements = new String[] { "Cat", "Dog", "Cow", "Horse", "Pig", "Monkey" };
    DefaultListModel<String> listModel = new DefaultListModel<String>();
    for (String element : listElements) {
        listModel.addElement(element);
    }
    return listModel;
}


In the following method there is a logic error in it because as soon as the mouse button is released, the dragging has to be stopped. In plain language, the IF condition must be negated simply

@Override
    public void mouseReleased(MouseEvent e) {
        if (!mouseDragging) {        
            int dragTargetIndex = myList.getSelectedIndex();
            String dragElement = myListModel.get(dragSourceIndex);
            myListModel.remove(dragSourceIndex);
            myListModel.add(dragTargetIndex, dragElement);
        }
        mouseDragging = false;
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜