【问题标题】:Recursively add ArrayList data into JTable递归地将 ArrayList 数据添加到 JTable 中
【发布时间】:2015-08-13 18:46:53
【问题描述】:

我知道这是初学者的问题,但它不起作用。这是代码

public void imageshow( String path ) throws IOException {
    File root = new File( path );
    File[] list = root.listFiles();
    if (list == null) return;

    for ( File f : list ) {
       imageshow(f.getAbsolutePath());

         if(f.getName().endsWith("jpg")||f.getName().endsWith("png")||f.getName().endsWith("gif")||f.getName().endsWith("tif"))
                {
                images=new ArrayList<String>();
                DefaultTableModel model=new DefaultTableModel();
   model.addColumn("Imya");

   table.setModel(model);
                model.addRow(new Vector(images));
                images.add(f.getName());
                image_count++;
                for(String img:images)
                {
                    System.out.println(img);
                }
            }
        }
    }

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    DirectoryReader fw = new DirectoryReader();
    System.out.println("---Images----");

    try {
        fw.imageshow("D:\\Installs\\shohruh\\doc");
    } catch (IOException ex) {
        Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
    }
 }

所以,我希望在单击按钮时它应该递归地添加到我的 JTable 中。我错过了哪些细节?从逻辑上讲,我正确地编写了代码。但我没有显示在 JTable 上!请帮助解决这个问题。提前致谢

【问题讨论】:

  • 源代码中的一个空白行是永远需要的。 { 之后或 } 之前的空行通常也是多余的。

标签: java swing arraylist jtable


【解决方案1】:

可能是因为在每个循环中都初始化了 ArrayList:

images=new ArrayList<String>();

当你想使用递归算法时,你应该把它放在递归方法之外(作为实例变量或其他东西)。 然后,有声明:

model.addRow(new Vector(images));

但是图片列表还是空的


编辑
private DefaultTableModel model=new DefaultTableModel();
private ArrayList<ArrayList<String>> image=new ArrayList<ArrayList<String>>();
public void imageshow( String path ) throws IOException {
File root = new File( path );
File[] list = root.listFiles();

if (list == null) return;

for ( File f : list ) {
   imageshow(f.getAbsolutePath());

     if(f.getName().endsWith("jpg")||f.getName().endsWith("png")||f.getName().endsWith("gif")||f.getName().endsWith("tif"))
            {
            ArrayList<String> row = new ArrayList<String>();
            row.add(f.getName());
            images.add(row);
            image_count++;
        }
    }
}

当你打电话时:

model.addColumn("Imya");
table.setModel(model);
for(ArrayList<String> list:images)
     model.addRow(new Vector(list));

未测试


编辑 - 整个代码:
private DefaultTableModel model=new DefaultTableModel();
private ArrayList<ArrayList<String>> image=new ArrayList<ArrayList<String>>();
public void imageshow( String path ) throws IOException {
    File root = new File( path );
    File[] list = root.listFiles();
    if (list == null) return;
    for ( File f : list ) {
        imageshow(f.getAbsolutePath());
        if(f.getName().endsWith("jpg")||f.getName().endsWith("png")||f.getName().endsWith("gif")||f.getName().endsWith("tif"))
            {
            ArrayList<String> row = new ArrayList<String>();
            row.add(f.getName());
            images.add(row);
            image_count++;
        }
    }
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
    DirectoryReader fw = new DirectoryReader();
    images.clear();
    System.out.println("---Images----");
    try {
         fw.imageshow("D:\\Installs\\shohruh\\doc");
    } catch (IOException ex) {
        Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
    }
    model.addColumn("Imya");
    table.setModel(model);
    for(ArrayList<String> list:images)
         model.addRow(new Vector(list));
}

格式不正确,未经测试,由 JavaDoc 编写

【讨论】:

    【解决方案2】:

    为您提供示例代码。

    String[] usernames = new String[]{"dev","root","developer","lastroot"};
    
                Collections.reverse(Arrays.asList(usernames));
                jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(usernames));
    

    使用前拍照

    Collections.reverse(Arrays.asList(usernames));
    

    -- snaps 使用后

     Collections.reverse(Arrays.asList(usernames));
    

    希望它对你有用。

    【讨论】:

    • 好的,先生,这是链接:hightail.com/download/bXBiS3dxZy9CSnJOTzhUQw
    • 请将cataloger.DirectoryReader依赖jar的链接发给我。
    • 先生,这是我项目的整个包。请运行 CatalogerMainScreen.java 文件,因为它是主文件。感谢 hightail.com/download/bXBiS3dsSWhCSWV5VmNUQw
    猜你喜欢
    • 2013-12-29
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多