Click to See Complete Forum and Search --> : JTable: Preventing cell from being edited.


SkyM
October 13th, 1999, 04:24 AM
I've got created JTable, and I'd like to prohibit cell's editing (make all cell read-only). How can I do this? Please, help!

Thanks in advance,
SY, SkyM

poochi
October 13th, 1999, 09:54 AM
isCellEditable() method of TableModel has to return false.

Try this ..



class CalendarTableModel extends AbstractTableModel{
String[] m_tColumnName = {"A" ,"B" , "C" , "D" , "E" , "F" , "G" };
int m_nRowCount = 0;
protected Vector dataVector;

public CalendarTableModel(){
dataVector = new Vector();
setNumRows(6);
}

public int getRowCount(){
return dataVector.size();
}

public int getColumnCount(){
return m_tColumnName.length;
}

public Object getValueAt( int nRow , int nColumn ){
Vector rowVector = (Vector)dataVector.elementAt(nRow);
return rowVector.elementAt(nColumn);
}

public void setValueAt( Object tValue ,int nRow , int nColumn ){
Vector rowVector = (Vector)dataVector.elementAt(nRow);
rowVector.setElementAt(tValue, nColumn);

// generate notification
fireTableChanged(new TableModelEvent(this, nRow , nRow , nColumn));
}

public String getColumnName( int nColumnIndex ){
return m_tColumnName[nColumnIndex];
}

public boolean isCellEditable( int nRow , int nColumn ){
return false;
}
public void setNumRows(int newSize) {
if ((newSize < 0) || (newSize == getRowCount()))
return;

int oldNumRows = getRowCount();
if (newSize <= getRowCount()) {
// newSize is smaller than our current size, so we can just
// let Vector discard the extra rows
dataVector.setSize(newSize);

// Generate notification
fireTableRowsDeleted(getRowCount(), oldNumRows-1);
}
else {
int columnCount = getColumnCount();
// We are adding rows to the model
while(getRowCount() < newSize) {
Vector newRow = new Vector(columnCount);
newRow.setSize(columnCount);
dataVector.addElement(newRow);
}

// Generate notification
fireTableRowsInserted(oldNumRows, getRowCount()-1);
}
}
}





I dont know a simple solution for this.

Poochi...

SkyM
October 14th, 1999, 01:14 AM
Thanks for help.

newkid
October 19th, 1999, 11:28 PM
can you provide the full source code for a working abstract model ?

thanks

http://members.xoom.com/lookads

poochi
October 19th, 1999, 11:59 PM
????? What's wrong with that code ? That's a fully working code.