1

enter image description here

I want to achieve look and feel like in the image above.

Here is the code I wrote, but I don't know how to make it look like this table.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.xml.crypto.Data;

import com.cdac.FileNameCopy;

class TableCheckBox extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public void TableCheckBoxfunc(String fileName,String Ext,boolean selection) 
    {
        Object[] columnNames = {"File Names", "Extentions","Selection"};
        Object[][] data = { {fileName,Ext,selection}};

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {

          @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:   return String.class;
                    case 1:   return String.class;
                    default:  return Boolean.class;
                }
            }
        };

        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        c.add(new JScrollPane(table), BorderLayout.CENTER);


        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);


        return ;
    }

    public static void main(String[] args) 
    {

        boolean selection=false;
        String fileName="";
        String extension="";


        File folder = new File("c:\\file");
        File[] listOfFiles = folder.listFiles();


        TableCheckBox frame = new TableCheckBox();

        for (int i = 0; i < listOfFiles.length; i++) 
        {
              if (listOfFiles[i].isFile()) {

                  final String fullName=listOfFiles[i].getName();
                  int a = fullName.lastIndexOf(".");
                  int len=fullName.length();

                  fileName=fullName.substring(0,a);
                  extension=fullName.substring(a+1,len);

                  System.out.println("name of file is :"+fileName);
                  System.out.println("Extension of file is :"+extension);




                System.out.println("File " + fullName);
              } else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory " + listOfFiles[i].getName());
              }
              frame.TableCheckBoxfunc(fileName, extension, selection);
                // frame.TableCheckBoxfunc(fileName, extension, selection);;
              frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
              frame.pack();
              frame.setLocation(150, 150);
              frame.setVisible(true);
        }

    }
}

2 Answers 2

1

This will be very helpful. http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

What I don't understand in your code is this

Object[][] data = { {fileName,Ext,selection}};

I would expect something like this

Object[][] data = {
    {"S001", "Alice", 90, true}
    //...more rows
}
Sign up to request clarification or add additional context in comments.

Comments

1

First of all populate the model data Object[][] then finally add it in the DefaultTableModel and create JTable just once.

Sample code:

Object[][] data = new Object[listOfFiles.length][3];
for (int i = 0; i < listOfFiles.length; i++) {
    ...
    data[i][0] = fileName;
    data[i][1] = extension;
    data[i][2] = selection;
}

Object[] columnNames = { "File Names", "Extentions", "Selection" };
DefaultTableModel model = new DefaultTableModel(data, columnNames);
// create JTable just once and pass model in the constructor

How to Use Tables? learn more...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.