11

I created a function which loads data into a JTable. Everything's working fine except that all the cells in this table are editable. Btw, I used defaultTableModel for the table model. Im doing this in Netbeans IDE. Please help. Here's my code:

private void updateTable(String searchText){

    if(searchText != null)
        this._sqlCmd = this._sqlCmd + " WHERE "+columnCombo.getSelectedItem()+" LIKE '%"+searchText+"%'";
    jTable1.setSurrendersFocusOnKeystroke(true);
    table = (javax.swing.table.DefaultTableModel) jTable1.getModel();  

    try{
        table.setRowCount(0);
    }catch(Exception e){}

    try {
        ResultSet rs = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(_sqlCmd);
        while (rs.next()){
            Object[] data = new Object[numOfCols];
            for(int i=0; i<data.length; i++){
                data[i] = rs.getObject(i+1);
            }
            table.addRow(data);
        }
        table.fireTableDataChanged();


    } catch (SQLException ex) {
        Logger.getLogger(FindContactGrid.class.getName()).log(Level.SEVERE, null, ex);
    } 
}
Sam
  • 7,252
  • 16
  • 46
  • 65
John
  • 836
  • 2
  • 25
  • 39
  • This is the code updating the data model. Editable cells have to do with the JTable instead. – maple_shaft May 03 '12 at 13:30
  • I strongly suggest to avoid empty catch blocks, certainly if you catch any possible `Exception` in the catch – Robin May 03 '12 at 13:51
  • possible duplicate of [How to make a JTable non-editable](http://stackoverflow.com/questions/1990817/how-to-make-a-jtable-non-editable) – Math Jan 27 '14 at 11:41

6 Answers6

34
 private TableModel model = new DefaultTableModel(data, columnNames)
  {
    public boolean isCellEditable(int row, int column)
    {
      return false;//This causes all cells to be not editable
    }
  };
  private JTable table = new JTable(model);

Edited. If you are doing this in Netbeans IDE designer, follow the steps below:

  • Select the form on which the JTable is placed
  • From the Navigation Pane, expand JScrollPane and right-click on JTable and Select Customize Code as shown below:

Navigator Pane

  • On the code customizer, select the second drop down and choose custom property. This enables you to edit the DefaultTableModel code definition.
  • Now paste this: {public boolean isCellEditable(int row, int column){return false;}} before the last closing blacket );

Your final setup should look as shown below:

  • Press ok to save - and job done.

Code Customizer

Bitmap
  • 12,402
  • 16
  • 64
  • 91
  • thanks but I don't think this will work in my code. the default table model that I'm using was created by netbeans. Unfortunately, I cant edit the code. Is there another way for this? – John May 03 '12 at 13:38
  • You can do it in Netbeans `@alain.janinm` has already posted how to do this in Netbean designer. – Bitmap May 03 '12 at 13:55
  • `@user1349213` see edited version of my answer on how to do this in Netbeans. – Bitmap May 03 '12 at 15:07
9

If you use DefaultTableModel you can override the method isCellEditable and implement it when constructing GUI:

table.setModel(new DefaultTableModel() {

    @Override
    public boolean isCellEditable(int row, int column) {
       return false;
    }
});
Cloud
  • 983
  • 11
  • 11
2

Using Netbeans ANOTHER WAY is possible. if you want to continue using the default table model as the OP mentions It's not necessary to create a new table model if you don't want to.

  1. Select the JTable "properties"

  2. Select the "TableModel" field in "properties" which will open another DialogBox.

  3. From there it is possible to modify the "editable" checkbox field for each column.

Not sure from which version this starts but I'm using Netbeans 7.2

2

Try This

JTable table = new JTable();
table.setEnabled(false);
Izanagi
  • 19
  • 1
1

As other said you have to create you own DefaultTableModel and override isCellEditable. In order to use it in Netbeans designer :

  • Right click on your table
  • Properties -> Code
  • In Custom Creation Code add this : new JTable(new MyModel()) (assuming you create class MyModel extends AbstractTableModel)
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
-1
JTextField f = new JTextField();
f.setEditable(false);
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
    table.getColumnModel().getColumn(i).setCellEditor(new DefaultCellEditor(f));
}
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73