Mike
Mike

Reputation: 43

Recursively add ArrayList data into JTable

I know this is beginner's question but it doesn't work. Here's the code

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);
    }
 }

So , I want when the button is clicked it should recursively add to my JTable. Which details was I missed? Logically, I wrote a code correctly. But I doesn't displayed on JTable! Please help to fix this problem. Thanks in advance

Upvotes: 2

Views: 179

Answers (2)

maskacovnik
maskacovnik

Reputation: 3084

Maybe because of initializing ArrayList in each loop:

images=new ArrayList<String>();

When you want to use recursive alogrithm, you should put this outside the recursive method (as instance variable or something). Then, there is statement:

model.addRow(new Vector(images));

but images list is still empty


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++;
        }
    }
}

And when you are calling:

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

Not tested


EDIT - whole code:

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));
}

Not well formated and not tested, written by JavaDoc

Upvotes: 1

Avyaan
Avyaan

Reputation: 1313

sample code for you.

String[] usernames = new String[]{"dev","root","developer","lastroot"};

            Collections.reverse(Arrays.asList(usernames));
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(usernames));

snaps before using

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

enter image description here

-- snaps After using

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

enter image description here

Hope it may work for you.

Upvotes: 2

Related Questions