递归地将ArrayList数据添加到JTable中

时间:2015-05-31 05:56:03

标签: java swing arraylist jtable

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

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上显示!请帮助解决这个问题。提前致谢

2 个答案:

答案 0 :(得分: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));

enter image description here

- 使用

后快照
 Collections.reverse(Arrays.asList(usernames));

enter image description here

希望它可能适合你。

答案 1 :(得分:1)

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

images=new ArrayList<String>();

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

model.addRow(new Vector(images));

但图片列表仍为空


EDIT

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编写的格式不完整且未经过测试

相关问题