Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Set JTable non editable

Posted on 2011-03-22
14
959 Views
Last Modified: 2012-08-14
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
Comment
Question by:Aurii
  • 6
  • 5
  • 3
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35188798
0
 

Author Comment

by:Aurii
ID: 35189727
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 35189798
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:Aurii
ID: 35189970
Thank you its' worked. But if I need some cell editable and some non editable then how can can I do it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35190414
You need to return true/false for the appropriate cell
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 35194923
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
 

Author Comment

by:Aurii
ID: 35196281
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 35196450
'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
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 35197094
> 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
 

Author Comment

by:Aurii
ID: 35230323
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
 
LVL 92

Expert Comment

by:objects
ID: 35230342
explained that in my previous comment
you would use

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

Author Comment

by:Aurii
ID: 35230547
but this statement giving me following error,
'operator && cannot be applied to boolean,int'
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 35230559

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

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35230854
:)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 42
Way to decrease size of apk file 9 86
sql import cannot be resolved jsp 3 47
web project error add remove 1 28
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question