Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamically make Jtable row editable???

1,705 views
Skip to first unread message

sgubba

unread,
Sep 7, 2000, 10:28:52 PM9/7/00
to
I have a problem making the JTable editable.I have the
TableModel class derived from AbstractTabelModel and I am
initially returning false from the function isCellEditable() to
disable editing. Now I want to enable the selected row of the
JTable editable on clicking an edit button.

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

Andreas Schnell

unread,
Sep 8, 2000, 2:53:11 AM9/8/00
to

"sgubba" <sgubbaN...@sun.partner.remarq.com.invalid> schrieb im
Newsbeitrag news:00000119...@usw-ex0108-192.remarq.com...

> I have a problem making the JTable editable.I have the
> TableModel class derived from AbstractTabelModel and I am
> initially returning false from the function isCellEditable() to
> disable editing. Now I want to enable the selected row of the
> JTable editable on clicking an edit button.
>
> Any suggestions please.

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


sgubba

unread,
Sep 8, 2000, 7:03:12 PM9/8/00
to
Thanks Schnell. But I don't have a reference to the JTable in
my tableModel. Your idea helped me implementing this.

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);
}
}

Dennis Bulai

unread,
Sep 12, 2000, 11:45:15 AM9/12/00
to
It would seem that you could do this in the isCellEditable() method. Have
it check a flag that is set by the edit button, get the selected row from
selection model and have it return true for all cells in that row, something
like this
ListSelectionModel lsm = myTable.getSelectionModel()
int rowSelected = lsm.getMinimumSelectionIndex();

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>...

0 new messages