mercredi 18 novembre 2015

Add a checkbox to a JTable which has been generated from a CSV

I have a function which loops through a CSV file, and generates a DefaultTableModel of the data (which is then displayed in a JTable). This function is:

public DefaultTableModel createTableModel(Reader in, Vector<Object> headers) {
        DefaultTableModel model = null;
        Scanner s = null;
        Vector<Object> c;
        try {
            Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
            s = new Scanner(in);
            while (s.hasNextLine()) {
                rows.add(new Vector<Object>(
                        Arrays.asList(s.nextLine().split("\\s*,\\s*", -1))));
            }
            if (headers == null) {
                headers = rows.remove(0);
                model = new DefaultTableModel(rows, headers);
            } else {
                model = new DefaultTableModel(rows, headers);
            }
            return model;
        } finally {
            s.close();
        }
    }

However, I would like to modify this code to add a checkbox to the first column of data (which I have added as a template when generating the CSV file anyways as a column with header "Selected"), which fills in the boolean false for each row.

The problem is, I don't know where in my code something like the below should go (after reading online I see that this is the way you can get a JTable to autogenerate selection textboxes, by setting the class of a column to Boolean). Note, here the "Selected" column is column 1 hard coded:

{
    public Class getColumnClass(int column) {
         if(column==1){
                        return Boolean.class;
         }else return String.class;
      }
 };

Should be defined (I am presuming it should be in the function above).




Aucun commentaire:

Enregistrer un commentaire