Checkbox doesnt change when click on JTable
im building a custom Table for an application and everything is working except that the checkbox doesnt change when you click the mouse on top of it. Im new at java and really dont know what im doing wrong,
Thank you in advance
Code Java:
/*
* 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) {
File f = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial.txt");
File g = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial1.txt");
File h = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial2.txt");
File i = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial3.txt");
File j = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial4.txt");
System.out.println(f.exists()); //checks if file exists
JFrame frame = new JFrame("Table Test");
FileTableModel data = new FileTableModel();
JTable table = new JTable();
table.setModel(data);
data.addData(f);
data.addData(g);
data.addData(h);
data.addData(i);
data.addData(j);
System.out.println(data);
System.out.println("Data Added");
JScrollPane scrollpane = new JScrollPane(table);
frame.getContentPane().add(scrollpane,BorderLayout.CENTER);
frame.setSize(300,150);
frame.setVisible(true);
}
}
class FileTableModel extends AbstractTableModel {
private String[] columnNames = { "Selection", "File Name","File Path" };
FileData rowData;
FileTableModel()
{
rowData = new FileData();
}
@Override
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return rowData.getListSize();
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col) {
if (col == 0)
{
return rowData.getSelection();
}
if (col ==1)
{
return rowData.getFileName(row);
}
if (col == 2)
{
return rowData.getFilePath(row);
}
else return "Invalid Row or Column";
}
@Override
public boolean isCellEditable(int row, int col)
{
if (col == 0)
return true;
else
return false;
}
@Override
public void setValueAt(Object value, int row, int col)
{
}
public void addData(File f)
{
rowData.addData(f);
}
public void removeData(int row)
{
rowData.removeData(row);
}
}
class FileData {
private LinkedList <File> FileList = new LinkedList <File>();
private Boolean selection = false;
FileData()
{
}
public int getListSize()
{
return FileList.size();
}
public String getFileName(int row)
{
return FileList.get(row).getName();
}
public String getFilePath(int index)
{
return FileList.get(index).getAbsolutePath();
}
public Boolean getSelection()
{
return selection;
}
public void addData(File f)
{
FileList.add(f);
}
public void removeData(int row)
{
FileList.remove(row);
}
}
Re: Checkbox doesnt change when click on JTable
Please edit your code and wrap it in code tags. The currently posted code is very hard to read.
See: BB Code List - Java Forums
Re: Checkbox doesnt change when click on JTable
Sorry About that! here it is
Code Java:
/*
* 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) {
File f = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial.txt");
File g = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial1.txt");
File h = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial2.txt");
File i = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial3.txt");
File j = new File("//home//guest//rosado//NetBeansProjects//PriKL_Platform//TrialFileTableModel//src//trialfiletablemodel//TableModelTrial4.txt");
System.out.println(f.exists()); //checks if file exists
JFrame frame = new JFrame("Table Test");
FileTableModel data = new FileTableModel();
JTable table = new JTable();
table.setModel(data);
data.addData(f);
data.addData(g);
data.addData(h);
data.addData(i);
data.addData(j);
System.out.println(data);
System.out.println("Data Added");
JScrollPane scrollpane = new JScrollPane(table);
frame.getContentPane().add(scrollpane,BorderLayout .CENTER);
frame.setSize(300,150);
frame.setVisible(true);
}
}
class FileTableModel extends AbstractTableModel {
private String[] columnNames = { "Selection", "File Name","File Path" };
FileData rowData;
FileTableModel()
{
rowData = new FileData();
}
@Override
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return rowData.getListSize();
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col) {
if (col == 0)
{
return rowData.getSelection();
}
if (col ==1)
{
return rowData.getFileName(row);
}
if (col == 2)
{
return rowData.getFilePath(row);
}
else return "Invalid Row or Column";
}
@Override
public boolean isCellEditable(int row, int col)
{
if (col == 0)
return true;
else
return false;
}
@Override
public void setValueAt(Object value, int row, int col)
{
}
public void addData(File f)
{
rowData.addData(f);
}
public void removeData(int row)
{
rowData.removeData(row);
}
}
class FileData {
private LinkedList <File> FileList = new LinkedList <File>();
private Boolean selection = false;
FileData()
{
}
public int getListSize()
{
return FileList.size();
}
public String getFileName(int row)
{
return FileList.get(row).getName();
}
public String getFilePath(int index)
{
return FileList.get(index).getAbsolutePath();
}
public Boolean getSelection()
{
return selection;
}
public void addData(File f)
{
FileList.add(f);
}
public void removeData(int row)
{
FileList.remove(row);
}
}
Re: Checkbox doesnt change when click on JTable
The setValueAt method if your TableModel does nothing...implement this method to change the value that needs changing using the parameters and setting the appropriate value in your model