1

I want to make my JTable Non-editable

As I use following code to set rows using SetModel():

jTable1.setModel(DbUtils.resultSetToTableModel(rs)); //Resultset is added as each row using r2xml JAR file

I cant use follwing code:

jTable1.setModel(new DefaultTableModel() {

    @Override
    public boolean isCellEditable(int row, int column) {
       return false;
    }
});

Because we cannot use two setModel() for jTable.

How to overcome this problem? I want to setresult and make jTable Noneditable.

2
  • What you want to do with the values modified by the user? Store in a database directly? Hold the modifications transient in memory and store on commiting? Sep 29, 2014 at 11:13
  • I want to make Jtable Noneditable after i fetch rows from DB using resultSettoTable(rs)
    – Aj Kumar
    Sep 29, 2014 at 11:20

2 Answers 2

4

Here are 2 ways to achieve that:

  1. Create and use your own TableModel implementation which forwards all calls to the table model returned by DbUtils except for isCellEditable() in which you can return always false hence disabling editing. Your own table model could get the model returned by DbUtils as a constructor argument for example.

  2. You can extend JTable and override its isCellEditable() method to return false (by default it calls the model's isCellEditable() method). Maybe other Swing enthusiasts will see this as an evil hack, but it is the simplest solution to your problem here.

Elaborating method #1

This is how you can create your model:

class MyModel implements TableModel {
    private final TableModel m;
    public MyModel(TableModel m) {
        this.m = m;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        // This is how we disable editing:
        return false;
    }

    // The rest of the methods just forward to the other model:

    @Override
    public int getRowCount() {
        return m.getRowCount();
    }

    @Override
    public int getColumnCount() {
        return m.getColumnCount();
    }

    // ...and all other methods which I omit here...
}

And this is how you can use it:

jTable1.setModel(new MyModel(DbUtils.resultSetToTableModel(rs)));

Elaboration of method #2

Extending JTable can even be an anonymous class:

JTable jtable1 = new JTable() {
    @Override
    public boolean isCellEditable(int row, int column) {
        // This is how we disable editing:
        return false;
    }
};

And using it:

// You can set any model, the table will not be editable because we overrode
// JTable.isCellEditable() to return false therefore the model will not be asked
// if editable.

jTable1.setModel(DbUtils.resultSetToTableModel(rs));
9
  • if i use isCellEditable(),then how i can set it to jtable
    – Aj Kumar
    Sep 29, 2014 at 11:08
  • Beacause i cannot use two setModel() for single jTable
    – Aj Kumar
    Sep 29, 2014 at 11:08
  • First option will not work without to implement setValueAt method. Sep 29, 2014 at 11:09
  • @user3360047 You only have to call setModel() once, with your custom model (which uses the model returned by DbUtils). I edited my answer to elaborate it.
    – icza
    Sep 29, 2014 at 11:20
  • @SergiyMedvynskyy The custom model would forward the setValueAt() call to the other model just like all other methods. I don't see a problem here.
    – icza
    Sep 29, 2014 at 11:21
0

you can use this code for make non editable jTable simply you write one line in your program jTable.disable();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.