How to control a column, a cell, or not allow to be edited in JTable

Source: Internet
Author: User
Tags implement

There are a lot of articles on how to implement JTable that can't be edited, because if you don't, double-clicking the cell will read the contents of the cell into a default editor, and many times we don't want it to happen, so we have to do something to stop it. Oh, well, I admit that we all know that the following method can be implemented so that the entire table is not allowed to be edited

DefaultTableModel Newtablemodel = new DefaultTableModel (datavector,columnmodelvector) {  
            @Override
            public Boolean iscelleditable (int row,int column) {return  
                false;  
            }  
        };

There is nothing mystical about rewriting the DefaultTableModel method isCellEditable method, but in more cases, we need to specify that a column or columns in the table are allowed to be edited, such as the number of items in the list, the price, and so on, so how do you implement it?

In fact, a closer look, this iscelleditable method will pass two parameters come in, a discerning eye to know that this is the cell row and column index, so, according to this row and columns to control return TRUE or FALSE, our desire can be achieved. Here are a few examples to learn to extrapolate OH:

1, only allow the third column of the table to be edited

public boolean iscelleditable (int row,int column) {  
    if (column = = 3) {return  
       true;  
    } else{return  
       false;  
    }  

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

2, only allow the second row of the table, the second column of cells are edited

public boolean iscelleditable (int row,int column) {  
   if (row = = 2 && column = = 2) {return  
       true;  
   } else{return  
       false;  
   }  

3. Allow only the 4th and 6th of the table to be edited

public boolean iscelleditable (int row,int column) {  
   if (row = = 4 | | | row = = 6) {return  
       true;  
   } else{return  
       false;  
   }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.