UnKn0wn
UnKn0wn

Reputation: 350

Java JTable - Make only one column editable

I was wondering how to make one column of a JTable editable, the other columns have to be non editable.

I have overwritten isCellEditable() but this changes every cell to non editable. Thnx in advance.

Upvotes: 18

Views: 48180

Answers (6)

AntPck
AntPck

Reputation: 1

You can also make a class which extends JTable and add a boolean array

public class MyTable extends JTable {
    private Boolean[] editable_columns;

    public DetailsTable(Object[][] rowData, Object[] columnNames) {
        super(rowData, columnNames);
        editable_columns = new Boolean[columnNames.length];
        for (int i = 0; i < columnNames.length; i++){
            editable_columns[i] = true;
        }
    }

Create your own method to modify the value for one column

public void setColumnEditable(int index, boolean isEditable){
        if (index > -1 && index < editable_columns.length){
            editable_columns[index] = isEditable;
        }
    }

And after Override setValueAt with your boolean array

@Override
    public void setValueAt(Object aValue, int row, int column) {
        if (editable_columns[column])
            super.setValueAt(aValue, row, column);
    }

Upvotes: 0

arash
arash

Reputation: 176

this would set editable true for column 3 and 8 and false for others .

DefaultTableModel model = new DefaultTableModel() {

            boolean[] canEdit = new boolean[]{
                    false, false, true, false, false,false,false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
};

Upvotes: 11

michel.iamit
michel.iamit

Reputation: 5906

Reading the remark of Kleopatra (his 2nd time he suggested to have a look at javax.swing.JXTable, and now I Am sorry I didn't have a look the first time :) ) I suggest you follow the link

I searched for an asnwer, and I combined several answers to my own solution: (however, not safe for all solutions, but understandable and quick impelmented, although I recommende to look at the link above)

You can keep it more flexible to set which column is editable or not later on, I used this for exmaple:

    columnsEditable=new ArrayList<Integer>();
    table=new JTable(new DefaultTableModel(){

            @Override
            public boolean isCellEditable(int row, int col) {
                if(columnsEditable.isEmpty()){
                    return false;
                }
                if(columnsEditable.contains(new Integer(col))){
                    return true;
                }
                return false;
          }
    });

And I used this function to set editable or not:

public void setColumnEditable(int columnIndex,boolean editable){
    if(editable){
        if(!columnsEditable.contains(new Integer(columnIndex))){
            columnsEditable.add(new Integer(columnIndex));
        }
    }else{
        if(columnsEditable.contains(new Integer(columnIndex))){
            columnsEditable.remove(new Integer(columnIndex));
        }
    }
}

Note: of course you have to define columnsEditable and JTable table global in this class:

private JTable table;
private ArrayList<Integer> columnsEditable;

Note 2: by default all columns are not editable, but that is my desired behaviour. If you whish otherwhise, either add all columns to columnsEditable or change the behaviour completely (make ArrayList columnsNonEditable in stead). In regard to Kleopatra's remark: its better not to use this last suggestion (but it depends on the used tablemodel and what you do in the rest of your program).

Upvotes: -1

EricR
EricR

Reputation: 1497

Override the table model

isCellEditable(int rowIndex, int columnIndex) takes two arguments, just return true for the column you want?

public boolean isCellEditable(int rowIndex, int columnIndex){
return columnIndex == 0; //Or whatever column index you want to be editable
}

Upvotes: 21

kleopatra
kleopatra

Reputation: 51524

JXTable/TableColumnExt of the SwingX project have api to configure editability per-table and per-column

 // make the table completely read-only
 xTable.setEditable(false);
 // make a column read-only
 xTable.getColumnExt(index).setEditable(false);

Note that it is only possible to narrow the editability compared to that returned by model.isCellEditable. That is you can make a editable cell read-only but not the other way round

Upvotes: 6

mKorbel
mKorbel

Reputation: 109815

you can set if is isEditable for TableColumn or TableColumn and TableCell too

@Override
public boolean isCellEditable(int row, int col) {
     switch (col) {
         case 0:
         case 1:
             return true;
         default:
             return false;
      }
}

Upvotes: 34

Related Questions