Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用按钮将文件添加到JList_Java_Swing_Jlist - Fatal编程技术网

Java 使用按钮将文件添加到JList

Java 使用按钮将文件添加到JList,java,swing,jlist,Java,Swing,Jlist,我正在尝试使用按钮将多个文件添加到JList。我可以打开文件选择器,但文件未保存在JList中。有人能帮我吗?这就是我到目前为止所做的: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser fc = new JFileChooser(); int result = fc.showOp

我正在尝试使用按钮将多个文件添加到JList。我可以打开文件选择器,但文件未保存在JList中。有人能帮我吗?这就是我到目前为止所做的:

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

    JFileChooser fc = new JFileChooser();
    int result = fc.showOpenDialog(this);
    if(result == JFileChooser.APPROVE_OPTION)
    {

    DefaultListModel mod = new DefaultListModel();
    
    JList jList1 = new JList();
    int f = jList1.getModel().getSize();
    mod.add(f, fc.getSelectedFile());
    }

}                                        

确保JList实际上有一个模型

将模型声明为字符串类型,以便不使用原始类型

在将文件名添加到JList之前,请确保该文件名不存在

使用addElement方法而不是add方法:


每次我尝试添加文件时,jList1中仍然没有显示任何文件?是否有任何错误?实际上,您是否正在尝试添加到JList?您使用的IDE和Java版本是什么?您要将文件添加到的特定JList是否实际命名为jList1?jList1是否包含在与JButton1AActionPerformed事件相同的类中?如果您的JList已包含诸如:项目1、项目2、项目3等元素,则通过您的属性窗格进入模型并将其删除并重置为default1以更快地获得更好的帮助,从而添加或。2 JList jList1=新JList;int f=jList1.getModel.getSize;当然f在这里总是0,你检查过值了,对吗?
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // Select a file from JFileChooser
    JFileChooser fc = new JFileChooser();
    int result = fc.showOpenDialog(this);

    if (result != JFileChooser.APPROVE_OPTION) {
        // If a file was not selected then get outta here
        return;
    }

    // Place the selected file name into a String variable.
    String fileName = fc.getSelectedFile().getName();

    // Make sure the JList contains a model (it is possible not to have)
    DefaultListModel<String> mod;
    try {
        // If this passes then the current model is 
        // acquired from the JList.
        mod = (DefaultListModel<String>) jList1.getModel();
    }
    catch (Exception ex) {
        // JList didn't have a model so, we supply one,
        jList1.setModel(new DefaultListModel<>());
        // then we aqcuire that model
        mod = (DefaultListModel<String>) jList1.getModel();
    }

    // Make sure the selected file is not already 
    // contained within the list.  
    boolean alreadyHave = false;
    for (int i = 0; i < mod.getSize(); i++) {
        if (mod.getElementAt(i).equals(fileName)) {
            alreadyHave = true;
            break;
        }
    }

    // If not already in List then add the file name.
    if (!alreadyHave) {
        mod.addElement(fileName);
    }
}