Im developing a program that has a JTable in it. The JTable should display a checkbox per row, the file name and the file path.
The problem im having is Im new to java and im having a hard time understanding how to make custom table model. This is what i have so far. Excuse me for all the bad coding in it. Thank you
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package trialfiletablemodel; import java.awt.BorderLayout; import java.io.File; import java.util.LinkedList; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; /** * String fileName,filePath; * @author rosado */ public class Main { public static void main(String[] args) { String fileName,filePath; File f = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial.txt"); System.out.println(f.exists()); //checks if file exists JFrame frame = new JFrame("Table Test"); TableData data = new TableData(); fileName = data.getFileName(f); filePath = data.getFilePath(f); JTable table = new JTable(new FileTableModel()); table.setValueAt(false, 0, 0); table.setValueAt(fileName, 0, 1); table.setValueAt(filePath, 0, 2); JScrollPane scrollpane = new JScrollPane(table); frame.getContentPane().add(scrollpane,BorderLayout.CENTER); frame.setSize(300,150); frame.setVisible(true); /* ArrayList <String> data = new ArrayList<String>(); data.add(f.getAbsolutePath()); data.add(f.getName()); for(int x = 0; x<data.size();x++) { System.out.println(data.get(x)); } */ } } class FileTableModel extends AbstractTableModel { public String[] columnNames = { "Selection", "File Name","File Path" }; LinkedList <Object> tabledata = new LinkedList<Object>(); public Class[] columnTypes = {Boolean.class,String.class,String.class}; //Constructor public FileTableModel(){ super(); } @Override public int getColumnCount() { return columnNames.length; } @Override public Class getColumnClass(int column){ return (getValueAt(0, column).getClass()); } @Override public String getColumnName(int column) { return columnNames[column]; } @Override public int getRowCount() { return (tabledata.size()); } @Override public Object getValueAt(int row, int col) { float index = (row/2); if ((index % 2) == 0) { index =(int) index; } else index = (int) index +1; return tabledata.get((int) index); } @Override public void setValueAt(Object value, int row, int column) { /* TableData d = new TableData(); Boolean fs = false; String fn = d.getFileName(f); String fp = d.getFilePath(f); tabledata.addLast(fs); tabledata.addLast(fn); tabledata.addLast(fp); */ } @Override public boolean isCellEditable(int row, int column) { if(column == 0){ return false; } else { return(true); } } } class TableData { private Boolean selection; private String fileName; private String filePath; public TableData() { } public TableData(Boolean sel, String name,String path) { selection = sel; fileName = name; filePath = path; } public Boolean getSelection() { return selection; } public String getFileName(File f) { fileName = f.getName(); return fileName; } public String getFilePath(File f) { filePath = f.getAbsolutePath(); return filePath; } public void setSelection(Boolean sel) { selection = sel; } public void setFileName(String name) { fileName = name; } public void setFilePath(String path) { filePath = path; } }