Jtable with first of JCheckBoxes

I need to have checkboxes on the first row of the Table and when I tick them i need to do something else (didn't got to that part yet). Anyway, so I have a vector of Booleans but I can only get to see true or false instead of the checkboxes.

publicclass DataFileTabelModelextends AbstractTableModel{

protected Vector<String> data;

protected Vector<Boolean> chkb;

protected Vector<String> columnNames ;

protected String datafile;

publicstaticint numlines=0;

publicstaticint numcol=0;

public DataFileTabelModel(String fname)throws IOException{

initVectors(fname);

}

publicvoid initVectors(String fname)throws IOException{

String aLine ;

chkb =new Vector<Boolean>();

data =new Vector<String>();

BufferedReader in =new BufferedReader(new FileReader(fname));

String str="";

int numlines=0;

int numcol=0;

//se numara liniile si coloane

while ((str = in.readLine()) !=null)

{

numlines++;

StringTokenizer st=new StringTokenizer(str," ");

numcol=st.countTokens();

}

float a[][]=newfloat[numlines][numcol];

columnNames =new Vector<String>();

Vector<String> subt=new Vector<String>();

String linie="";

BufferedReader br;

try{

br =new BufferedReader(new FileReader(fname));

while((linie=br.readLine())!=null)

subt.add(linie);

for(int i=0;i<subt.size();i++)

{

StringTokenizer st=new StringTokenizer(subt.get(i)," ");

for(int j=0;j<numcol;j++)

a[i][j]=Float.parseFloat(st.nextToken());

}

for(int i=0;i<numcol;i++){

columnNames.addElement("Col"+i);

chkb.addElement(Boolean.FALSE);

}

for(int i = 0;i >< numlines;i++ )

for(int j=0;j < numcol;j++ )

data.addElement(String.valueOf(a[i][j]));

}catch (Exception ex){

ex.printStackTrace();

}

}

publicint getRowCount(){

return data.size() / getColumnCount();

}

publicint getColumnCount(){

return columnNames.size();

}

public String getColumnName(int columnIndex){

String colName ="";

if (columnIndex <= getColumnCount())

colName = columnNames.elementAt(columnIndex);

return colName;

}

public Class getColumnClass(int columnIndex){

return getValueAt(0, columnIndex).getClass();

}

publicboolean isCellEditable(int rowIndex,int columnIndex){

returntrue;

}

publicvoid removeRow(int rowIndex)

{

for(int i = 0;i < getColumnCount();i++)

data.removeElementAt((rowIndex * getColumnCount()));

}

public Object fasule(int rowIndex,int columnIndex){

return data.elementAt

( (rowIndex * getColumnCount()) + columnIndex);

}

public Object getValueAt(int rowIndex,int columnIndex){

if (rowIndex==0)return chkb.elementAt( (rowIndex * getColumnCount()) + columnIndex);

elsereturn data.elementAt

( (rowIndex * getColumnCount()) + columnIndex);

}

publicvoid setValueAt(Object aValue,int rowIndex,int columnIndex){

data.setElementAt((String)aValue,(rowIndex * getColumnCount()) + columnIndex);

}

}

Where's the problem?

[7371 byte] By [CipNista] at [2007-11-26 23:50:29]
# 1

setDefaultRenderer(Class<?> columnClass, TableCellRenderer renderer)

Create a TableCellRenderer that returns a JCheckbox as the component. If you want these to be editable, you need to do the same with a TableCellEditor

I think it should already do it if getColumnClass returns a Boolean, check to make sure that Boolean.class is getting returned for that column.

Message was edited by:

robtaft

robtafta at 2007-7-11 15:28:11 > top of Java-index,Desktop,Core GUI APIs...
# 2
Simple example: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=637504> and when I tick them i need to do something else Add a TableModelListener to the TableModel. When the state of the Boolean value is changed, do you processing.
camickra at 2007-7-11 15:28:11 > top of Java-index,Desktop,Core GUI APIs...