Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a JTable editable in java

Tags:

java

swing

jtable

I'm using a JTable in java, but it won't let me edit the cells.

private final TableModel dataModel = new AbstractTableModel() {

        public int getColumnCount() { 
            return 5; 
        }

        public int getRowCount() { 
            return 10;
        }

        public Object getValueAt(int row, int col) { 
            return new Integer(row*col); 
        }
};

private final JTable table = new JTable(dataModel);
like image 990
Amre Avatar asked Apr 15 '13 05:04

Amre


2 Answers

add the follwoing code

 public boolean isCellEditable(int row, int col)
      { return true; }
 public void setValueAt(Object value, int row, int col) {
    rowData[row][col] = value;
    fireTableCellUpdated(row, col);
  }

you should have a array where you will save the changes

like image 93
aymankoo Avatar answered Sep 22 '22 12:09

aymankoo


Add isCellEditable() function inside the anonymous inner class AbstractTableModel

public boolean isCellEditable(int row, int col) { 
    return true; 
}
like image 33
Mohan Raj B Avatar answered Sep 19 '22 12:09

Mohan Raj B