Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 971
  • Last Modified:

Set JTable non editable

I am using JTable in my application. I want to set all cells of table non-editable. How can I do it? Please help.
0
Aurii
Asked:
Aurii
  • 6
  • 5
  • 3
5 Solutions
 
CEHJCommented:
0
 
AuriiAuthor Commented:
I am using netbeans. I took JTable 'tblSearch' by drag and drop and done below coding

private String rows[][] = new String[0][5];
    private String columns[] = {"Doctor ID","Doctor Name","Address","Contact Number","E-mail Id"};
    private javax.swing.table.DefaultTableModel tm = new javax.swing.table.DefaultTableModel(rows,columns);

tblSearch.setModel(tm);

The code from your example is not working for my code. Please suggest solution for my code.
0
 
CEHJCommented:
You need to do
private javax.swing.table.DefaultTableModel tm = new javax.swing.table.DefaultTableModel(rows,columns) {
    public boolean isCellEditable(int row, int col) {
        return false
    }
};

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AuriiAuthor Commented:
Thank you its' worked. But if I need some cell editable and some non editable then how can can I do it?
0
 
CEHJCommented:
You need to return true/false for the appropriate cell
0
 
objectsCommented:
see the example here
http://www.exampledepot.com/egs/javax.swing.table/NoEdit.html

to just make say solumn 1 not editable you could do something like this:


TableModel model = new DefaultTableModel() {
    public boolean isCellEditable(int rowIndex, int mColIndex) {
        return mColIndex!=1;
    }
};
0
 
AuriiAuthor Commented:
Hi CEHL, I am not geting how to return true/false for the appropriate cell for my code. I want to set all table non editable except some cells. Please suggest solution for my code.

Hi objects, Actually your code works for single column but what if I want to set it for multiple columns.

Actually my 1 table want all cells non editable except some cells And my another table want all cells non editable except some columns.
0
 
CEHJCommented:
'Some' needs to be specified numerically. Let's say you need to set the first, third and fifth columns editable, the rest not:
TableModel model = new DefaultTableModel() {
	public boolean isCellEditable(int rowIndex, int mColIndex) {
	    boolean result = false;
	    switch(rowIndex) {
		case 0:
		case 2:
		case 4:
		    result = true;
		default:
		    result = false;
	    }
	    return result;
	}
    };

Open in new window

0
 
objectsCommented:
> Hi objects, Actually your code works for single column but what if I want to set it for multiple columns.

just expand on what I posted above

eg. to only allow editting on columns 2 and 3

        return mColIndex==2 || mColIndex==3;

just change the condition to meet your needs
0
 
AuriiAuthor Commented:
Hi chej, Actually your code is working for hole row. what if I need to set some cell editable, let's say (2,3) and (5,8)
0
 
objectsCommented:
explained that in my previous comment
you would use

        return (mColIndex==2 && rowIndex=3) || (mColIndex==5 && mRowIndex=8);
0
 
AuriiAuthor Commented:
but this statement giving me following error,
'operator && cannot be applied to boolean,int'
0
 
CEHJCommented:

return (mColIndex==2 && rowIndex==3) || (mColIndex==5 && rowIndex==8);

Open in new window

0
 
CEHJCommented:
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now