未添加到 JList 的元素

发布于 2024-12-19 15:02:43 字数 1341 浏览 0 评论 0原文

我正在尝试将文件名添加到我的 JList 但没有成功。这是代码片段:


DefaultListModel model = new DefaultListModel();
listLayer.setModel(model);
model.addElement(file.getName());

listLayer 是一个 JList,我想在其中添加文件名。有关信息,我正在 netBeans 中编写 GUI 应用程序,因此我无法在此代码中创建新的 JList 对象,因为在将 JList 添加到我的布局时它已经自动创建了。因此我可以通过它的方法访问它。

多谢, 米哈尔.

-------------------------------------------------- -----------------------

好的,我会尝试更多地扩展它:

private void openActionPerformed(java.awt.event.ActionEvent evt) {                                     

    JFileChooser fileChooser = new JFileChooser("C:/");

    FileFilter filter1 = new MyCustomFilter();
    fileChooser.setFileFilter(filter1);

    int returnVal = fileChooser.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();

            //String[] tokens = file.getName().split(".");
            //String name = tokens[0];

            DefaultListModel model = new DefaultListModel();
            listLayer.setModel(model);
            model.addElement(file.getName());

    } else {
        System.out.println("File access cancelled by user.");
        }
} 

是的,我的名为 listLayer 的 JList 在代码的不可修改部分中声明,例如这: 私有 javax.swing.JList listLayer;

再次感谢您的帮助。米哈尔

I am trying to add file names to my JList but without success. Here is the piece of the code:


DefaultListModel model = new DefaultListModel();
listLayer.setModel(model);
model.addElement(file.getName());

listLayer is a JList into which I would like to add file name. For information, I am writing my GUI application in netBeans so I can not create a new JList object within this code as it was already created automatically when added JList to my layout. Therefore I can just access it through its methods.

Thanks a lot,
Michal.

-------------------------------------------------------------------------

Ok I will try to extend it more:

private void openActionPerformed(java.awt.event.ActionEvent evt) {                                     

    JFileChooser fileChooser = new JFileChooser("C:/");

    FileFilter filter1 = new MyCustomFilter();
    fileChooser.setFileFilter(filter1);

    int returnVal = fileChooser.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();

            //String[] tokens = file.getName().split(".");
            //String name = tokens[0];

            DefaultListModel model = new DefaultListModel();
            listLayer.setModel(model);
            model.addElement(file.getName());

    } else {
        System.out.println("File access cancelled by user.");
        }
} 

and yes, my JList called listLayer is declared in non-modifiable section of the code like this:
private javax.swing.JList listLayer;

Thanks again for any help. Michal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗琴音 2024-12-26 15:02:43

感谢您发布更多代码。现在我们很可能可以回答您的问题。我看到的一个问题是,每次按下按钮时,您都会重新创建一个 DefaultListModel,并使用这个新模型设置 JList,从而有效地删除列表之前保存的所有数据。避免这样做的一种方法是简单地获取 JList 已有的模型(应该是 DefaultListModel),并向其中添加项目。您需要转换 getModel() 返回的对象,因为根据 API,Java 只知道这是一个 ListModel 对象,而 ListModel 没有 addElement(...) DefaultListModel 所做的方法。

也许是这样的:

if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();

        // note the cast since getModel() only returns a ListModel
        DefaultListModel model = (DefaultListModel)listLayer.getModel(); // changed**

        model.addElement(file.getName());

} 

Thanks for posting more code. Now quite possibly we can answer your question. A problem I see is that you're recreating a DefaultListModel each time the button is pressed and setting the JList with this new model effectively removing all data that was previously held by the list. A way to avoid doing this is to simply get the model that the JList already has, which should be a DefaultListModel, and add items to it. You will need to cast the object returned by getModel() since per the API, Java only knows this to be a ListModel object, and ListModel doesn't have the addElement(...) method that DefaultListModel does.

Something perhaps like so:

if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();

        // note the cast since getModel() only returns a ListModel
        DefaultListModel model = (DefaultListModel)listLayer.getModel(); // changed**

        model.addElement(file.getName());

} 
话少情深 2024-12-26 15:02:43

您建议我不要使用 DefaultListModel,但是ListModel

我对你的程序的其余部分了解不够,无法说。如果您有一个相对静态的 JListDefaultListModel 可能是完美的。如果您的程序对不断变化的 File 实例选择进行建模,那么您可能需要实现 ListModel 甚至共享模型,如下所示 此处。后者只是将一些方法转发给默认实现。

You recommend me to use not DefaultListModel, but ListModel?

I don't know enough about the rest of your program to say. If you have a single, relatively static JList, DefaultListModel may be perfect. If your program models a constantly changing selection of File instances, then you may want to implement ListModel or even a shared model, as shown here. The latter simply forwards some methods to the default implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文