Any suggestions please.
I saw one of the member's "PhaniAmrut" posting the similar
problem to the forum. Phani, if you have the solution for this
please let me know. Thanks
Try this:
If you have your own TableModel you should have a reference to your JTable
and modifiy the method isCellEditable like
public boolean isCellEditable(int row, int column)
{
if(row == myTable.getSelectedRow())
return true;
else return false;
}
Hope this works
Andreas Schnell
This is the code. When user selects the Edit button, I am
making the selected row editable and the Edit button will be
changed to Ok button. once editing is complete, on Ok button i
am making the row readonly.
import com.sun.java.swing.JTable;
/**
* Insert the type's description here.
* Creation date: (9/8/00 12:02:36 PM)
* @author:
*/
class ReadWriteTableModel extends CustomTableModel {
private boolean editFlag = false;
private int rowToEdit = -1;
/**
* ReadWriteTableModel constructor comment.
*/
public ReadWriteTableModel() {
super();
}
/**
* ReadWriteTableModel constructor comment.
* @param columnHeaders java.lang.String[]
*/
public ReadWriteTableModel(String[] columnHeaders) {
super(columnHeaders);
}
/**
* gets the editFlag
*/
public boolean getEditFlag() {
return editFlag;
}
/**
* sets the table cells at a particular row and column to
editable or
* non-editable based on the return boolean value.
* false means non-editable, true means editable
* Creation date: (9/5/00 11:43:13 AM)
* @param int rowIndex - row index
* @param int colIndex - column index
* @return boolean - always returns false
*/
public boolean isCellEditable(int rowIndex, int colIndex) {
boolean edit = false;
// if the current row index is equal to the row which you
// want to edit, then make cell editable
if(editFlag == true && rowIndex == rowToEdit) {
edit = true;
}
else {
edit = false;
}
return edit;
}
/**
* sets the editFlag to true or false
*/
public void setEditFlag(boolean flag) {
editFlag = flag;
}
/**
* Insert the method's description here.
* Creation date: (9/8/00 1:22:04 PM)
* @param row int
*/
public void setRowToEdit(int row) {
rowToEdit = row;
}
}
/*****CustomTableModel*********/
/*****Readonly TableModel***********/
import com.sun.java.swing.table.AbstractTableModel;
import java.util.Vector;
/**
* Insert the type's description here.
* Creation date: (8/31/00 7:46:56 PM)
* @author:
*/
class CustomTableModel extends
com.sun.java.swing.table.AbstractTableModel {
// column names
private String[] headers;
// vector that holds all the row data of the jtable
private Vector rowVector = new Vector();
/**
* CustomTableModel constructor comment.
*/
public CustomTableModel() {
super();
}
/**
* Insert the method's description here.
* Creation date: (9/1/00 5:17:58 PM)
* @param columnHeaders java.lang.String[]
*/
public CustomTableModel(String[] columnHeaders) {
this();
headers = columnHeaders;
}
/**
* Add a row to the jtable with the specified object data
* Creation date: (8/31/00 8:05:47 PM)
* @param Object row containing data to be added
*/
public void addRow(Object rowData) {
int size = rowVector.size();
rowVector.addElement(rowData);
fireTableRowsInserted(size, size);
}
/**
* Gets the column count of the jtable
* Creation date: (8/31/00 8:05:47 PM)
* @return java.lang.int
*/
public int getColumnCount() {
return headers.length;
}
/**
* Gets the column name at the respective column no.
* Creation date: (8/31/00 8:05:47 PM)
* @return java.lang.String
* @param colNo int
*/
public String getColumnName(int colNo) {
return headers[colNo];
}
/**
* Gets the row count of the jtable
* Creation date: (8/31/00 8:05:47 PM)
* @return java.lang.int
*/
public int getRowCount() {
return rowVector.size();
}
/**
* Gets the value at a specified row & column in the
jtable
* Creation date: (8/31/00 8:05:47 PM)
* @return java.lang.Object
* @param int table row
* @param int table column
*/
public Object getValueAt(int row, int column) {
Vector rowObj = (Vector) rowVector.elementAt
(row);
return rowObj.elementAt(column);
}
/**
* sets the table cells at a particular row and column to
editable or
* non-editable based on the return boolean value.
* false means non-editable, true means editable
* Creation date: (9/5/00 11:43:13 AM)
* @param int rowIndex - row index
* @param int colIndex - column index
* @return boolean - always returns false
*/
public boolean isCellEditable(int rowIndex, int colIndex) {
return false;
}
/**
* removes all rows in the JTable
* Creation date: (9/5/00 5:27:22 PM)
*/
public void removeAllRows() {
int size = rowVector.size();
rowVector.removeAllElements();
fireTableRowsDeleted(0, size-1);
}
/**
* removes the specified row object in the JTable
* Creation date: (9/5/00 4:55:03 PM)
* @param Object obj - row object to be removed
*/
public void removeRow(Object obj) {
rowVector.removeElement(obj);
fireTableDataChanged();
}
/**
* removes the specified row in the JTable
* Creation date: (9/5/00 4:55:03 PM)
* @param row int - row index to be removed
*/
public void removeRowAt(int row) {
try {
rowVector.removeElementAt(row);
} catch(ArrayIndexOutOfBoundsException e) {
// System.error.println(e);
}
fireTableRowsDeleted(row, row);
}
/**
* Sets the column Headers with the specified column names
* Creation date: (9/1/00 5:00:28 PM)
* @param columnHeaders java.lang.String[]
*/
public void setColumnHeaders(String[] columnHeaders) {
headers = columnHeaders;
}
/**
* Sets the value at a specified row & column in the
jtable
* Creation date: (8/31/00 8:05:47 PM)
* @param Object the value to be set
* @param int table row
* @param int table column
*/
public void setValueAt(Object val, int row, int column)
{
Vector rowObj =(Vector) rowVector.elementAt(row);
rowObj.setElementAt(val, column);
fireTableRowsUpdated(row, row);
}
}
public booelan isCellEditable(int row, int col){
if(!editable){ // toggled by edit button
return false;
else if (editable && row == rowSelected){
return true;
}
}
This is just an idea off the top of my head so it may not work, but it
wouldn't take much time to give it a try.
Cheers
DB
sgubba wrote in message <00000119...@usw-ex0108-192.remarq.com>...