s_lavie
asked on
None editable cells in JTable
Hi,
Using a JTable with DefaultTableModel, I don't want to allow a user to edit any cell.
How do I do that?
I know there is a methode isCellEditable(int, int), but is there any setCellEditable(int, int), or something like this???
Using a JTable with DefaultTableModel, I don't want to allow a user to edit any cell.
How do I do that?
I know there is a methode isCellEditable(int, int), but is there any setCellEditable(int, int), or something like this???
There's no setCellEditable(int, int), you have to create a subclass of DefaultTableModel and override isCellEditable(int, int) to return false.
ASKER
It looks a little strange, don't you think?
There is a method isCellEditable(int, int) that returns a boolean i.e. true or false. Assuming that true is the default, shouldn't there be a nice and easy way to set that value?????
There is a method isCellEditable(int, int) that returns a boolean i.e. true or false. Assuming that true is the default, shouldn't there be a nice and easy way to set that value?????
I guess the idea here is that for a lot of tables tables some cells are editable while others are not (at least when I use them). If there would be a setCelEditable(int, int, boolean) you would have to call that method for every cell in the model.
The only way to change the setting is to override the DefaultTableModel.
The only way to change the setting is to override the DefaultTableModel.
ASKER
Ok, I agree that setCelEditable(int, int, boolean) to each cell dosn't make sense, but at least setTableEditable() to the whole table...
private void buildTable()
{
myTableModel = new AbstractTableModel()
{
public int getRowCount()
{
return myTableData.size();
}
public int getColumnCount()
{
return myHeaders.length;
}
public Object getValueAt(int row, int col)
{
Object[] entry = (Object[])myTableData.elem entAt(row) ;
return entry[col];
}
public String getColumnName(int col)
{
return myHeaders[col];
}
public boolean isCellEditable(int row, int col)
{
return false;
}
public void setValueAt(Object aValue, int row, int col)
{
Object[] entry = (Object[])myTableData.elem entAt(row) ;
entry[col] = aValue;
myTableData.setElementAt(e ntry,row);
}
};
}
the function isCellEditable returning false prevents all table cells from being edited by the user. i know you asked for the default model but it inherits the abstract model. this is what i use for all my basic tables. you set cells editable by specifing the return value.
ex.
isCellEditable func().
if(row == 2)
return true;
else
return false;
{
myTableModel = new AbstractTableModel()
{
public int getRowCount()
{
return myTableData.size();
}
public int getColumnCount()
{
return myHeaders.length;
}
public Object getValueAt(int row, int col)
{
Object[] entry = (Object[])myTableData.elem
return entry[col];
}
public String getColumnName(int col)
{
return myHeaders[col];
}
public boolean isCellEditable(int row, int col)
{
return false;
}
public void setValueAt(Object aValue, int row, int col)
{
Object[] entry = (Object[])myTableData.elem
entry[col] = aValue;
myTableData.setElementAt(e
}
};
}
the function isCellEditable returning false prevents all table cells from being edited by the user. i know you asked for the default model but it inherits the abstract model. this is what i use for all my basic tables. you set cells editable by specifing the return value.
ex.
isCellEditable func().
if(row == 2)
return true;
else
return false;
myTableData being a Vector;
myHeaders being a String[],
myColSizes being an int[]
myHeaders being a String[],
myColSizes being an int[]
ASKER
Hi roslyn,
Sorry but as far as I understand, your suggestion is a much more comlicated than mwibbels's, since in his suggestion I have to override only the methode isCellEditable(int, int), while in yours I have to do much more work.
Unless I got you wrong I'll have to reject your answer and accept mwibbels's, just to be fair...
Sorry but as far as I understand, your suggestion is a much more comlicated than mwibbels's, since in his suggestion I have to override only the methode isCellEditable(int, int), while in yours I have to do much more work.
Unless I got you wrong I'll have to reject your answer and accept mwibbels's, just to be fair...
ASKER
mwibbels, I wish to give you points - post an answer :-)
Sorry roslyn, his comment was better for my needs, and earlier... :-(
Sorry roslyn, his comment was better for my needs, and earlier... :-(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.