简体   繁体   English

Java Swing-使用TransferHandler动态更改JList

[英]Java Swing - Dynamically change JList using TransferHandler

I have a small Java swingui app where I display a JList and the user is able to cut, copy, paste and sort the list. 我有一个小型Java swingui应用程序,其中显示了JList,用户可以剪切,复制,粘贴和排序列表。

I use a custom TransferHandler to allow drag and drop on this Jlist. 我使用自定义TransferHandler允许在此Jlist上拖放。 Here is the code in building the JList, it basically builds it from an ArrayList. 这是构建JList的代码,它基本上是从ArrayList构建的。 "lstScripts" is the JList. “ lstScripts”是JList。

    ListTransferHandler lh = new ListTransferHandler();
...
    DefaultListModel listModelScripts = new DefaultListModel();
    for(Script s : scripts) {
        listModelScripts.addElement(s.getName());
    }
    this.lstScripts = new JList(listModelScripts);
    this.lstScripts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    this.lstScripts.addListSelectionListener(this);
    JScrollPane sp = new JScrollPane(this.lstScripts);
    sp.setPreferredSize(new Dimension(400,100));
    this.lstScripts.setDragEnabled(true);
    this.lstScripts.setTransferHandler(lh);
    this.lstScripts.setDropMode(DropMode.ON_OR_INSERT);
    setMappings(this.lstScripts);
...

On my custom TransferHandler class, I've got the importData routine working so that it handles the copy/paste/cut/sort. 在我的自定义TransferHandler类上,我已经使用importData例程,以便它可以处理复制/粘贴/剪切/排序。

public boolean importData(TransferHandler.TransferSupport info) {
    String scriptname = null; // The script name on the list

    //If we can't handle the import, bail now.
    if (!canImport(info)) {
        return false;
    }

    JList list = (JList)info.getComponent();
    DefaultListModel model = (DefaultListModel)list.getModel();
    //Fetch the scriptname -- bail if this fails
    try {
        scriptname = (String)info.getTransferable().getTransferData(DataFlavor.stringFlavor);
    } catch (UnsupportedFlavorException ufe) {
        System.out.println("importData: unsupported data flavor");
        return false;
    } catch (IOException ioe) {
        System.out.println("importData: I/O exception");
        return false;
    }

    if (info.isDrop()) { //This is a drop
        JList.DropLocation dl = (JList.DropLocation)info.getDropLocation();
        int index = dl.getIndex();
        model.add(index, scriptname);
        return true;
    } else { //This is a paste
        int index = list.getSelectedIndex();
        // if there is a valid selection,
        // insert scriptname after the selection
        if (index >= 0) {
            model.add(list.getSelectedIndex()+1, scriptname);
        // else append to the end of the list
        } else {
            model.addElement(scriptname);
        }
        return true;
    }
}

So up to here, everything works fine as far as the GUI. 因此,到目前为止,一切都可以在GUI上正常工作。 But my problem is I need the original JList "lstScripts" to be automatically updated with the user GUI changes. 但是我的问题是我需要原始的JList“ lstScripts”随用户GUI的更改而自动更新。 For example, if the user cuts or reorders the list, I want it to show on in "lstScripts". 例如,如果用户削减或重新排序列表,我希望它显示在“ lstScripts”中。

I'm not seeing how to make this connection between the TransferHandler and original GUI controller where "lstScripts" resides. 我没有看到如何在TransferHandler和“ lstScripts”所在的原始GUI控制器之间建立此连接。

@kleopatra - you helped me! @kleopatra-您帮助了我! sorry I didnt understand how the model was working. 抱歉,我不了解该模型的工作方式。

So in the controller, I create the "lstScripts" JList and add it to my panel (this is the first block of my code above). 因此,在控制器中,我创建了“ lstScripts” JList,并将其添加到面板中(这是上面代码的第一块)。

 pnlScripts.add(lstScripts, BorderLayout.WEST);

And as my code above showed, the listScripts JList had a custom transferhandler set as such: 就像我上面的代码所示,listScripts JList设置了一个自定义的transferhandler:

 this.lstScripts.setTransferHandler(lh);

So the transferhandler does all the user dnd (drag and drop) stuff. 因此,传输处理程序会执行所有用户dnd(拖放)操作。 In the controller, I can get the updated list by doing: 在控制器中,我可以通过执行以下操作获取更新的列表:

 DefaultListModel model = (DefaultListModel)lstScripts.getModel();
 for (int i = 0; i < model.getSize(); i++){
     scriptnames += model.getElementAt(i).toString() + ",";
 }

The scriptnames String variable now contains the updated list. 脚本名称String变量现在包含更新的列表。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM