Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to make JTable cell editable or uneditable dynamically

843806May 20 2008 — edited May 26 2008
Hi,
I have created a JTable using AbstractTableModel, Initially second column is uneditable and remaining column cells are editable. When JFrame is set visible, a button and table is displayed with second column uneditable.
When a Admin user clicks on button which is placed above table, a dialog box appears for admin authorization. When he logs in, the second column which is uneditable has to be made editable dynamically. For other users it should be uneditable. Iam not able to find exact code to make the cell editable depending on the user logs in.

The code i have written to create JTable is as follows.

class MyTableModel extends AbstractTableModel {
private String[] columnNames = { "SlNo", "Price", "Product"};

private Object[][] data = {
{ "1", "340", "Camera"},
{ "2", "500", "Mobile"},
{ "3", "400", "Walkman",},
{ "4", "20", "CD"},
{ "5", "100", "DVD"} };

public final Object[] longValues = { "Sharon", "Campione",
"None of the above", new Integer(20), Boolean.TRUE };

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

public int getRowCount() {
return data.length;
}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}


public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}


*public boolean isCellEditable(int row, int col) {*
* if(col==1)*
* return false;*
return true;
*}*
}

Need Suggestion or Sample code for the requirement.

Thanks in advance
Srikanth
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 23 2008
Added on May 20 2008
7 comments
1,433 views