0

Is there a way to update the current DefaultTableModel on a JTable? To clarify what I mean I will use some code examples below.

You might declare a JTable like this

DefaultTableModel model = new DefaultTableModel(tableData, tableHeaders)
{
    private static final long serialVersionUID = 8785594035471551113L;

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

JTable table = new JTable(model);
panel.add(table);

I am asking if there is a way to do something similar to this

myTable.java

public class myTable extends JTable {
    private static final long serialVersionUID = -5819940358496590055L;

    public myTable(TableModel dM) {
        setModel(dM);
    }

    public myTable(Object[][] tableData, Object[] columnHeaders) {
        this(new DefaultTableModel(tableData, columnHeaders));
    }

    public void setEditable(boolean b) {
        DefaultTableModel model = (DefaultTableModel) this.getModel();

        //Do some code here to make the editable or uneditable

        setModel(model);
    }
}

someJavaFile.java

JTable table = new myTable(tableData, tableHeaders);
table.setEditable(false);
panel.add(table);

I am not asking you to do this for me but I am asking you to aid me in finding a way to update the current DefaultTableModel.

I appreciate any help

Edit for clarification

I am trying to find a way to manipulate the current DefaultTableModel on a JTable for things such as whether the table is editable or not as there are no methods which support toggling whether the table is editable after the model has been made. There is only isCellEditable().

Why would this be useful?

This would be useful if you were wanting to manipulate the way the table worked with an ActionListener on a JButton or something similar

Dan
  • 7,286
  • 6
  • 49
  • 114
  • Keep a reference to the current `DefaultTableModel`. It has all the methods you'd need to update it. – Andrew Thompson Jul 10 '16 at 13:04
  • @AndrewThompson can you expand on this please for how it would help with this specific example as there is no `model.setEditable()` – Dan Jul 10 '16 at 14:17
  • Are you trying to make some cells not editable or something? – Frakcool Jul 10 '16 at 14:30
  • Oh, right.. OK. Define a new method for an extension of `DefaultTableModel` called `setEditable(boolean)`. Define a `private boolean editable;` as a class member and adjust it through that method. In the `isCellEditable()` method, change it to `return editable;`. – Andrew Thompson Jul 10 '16 at 14:32
  • 2
    I have done something similar on the code of [this question](http://stackoverflow.com/questions/17157915/how-to-center-cells-in-jtable) check if it works for you. It's similar to what @AndrewThompson wrote but for each column – Frakcool Jul 10 '16 at 14:37
  • 1
    To adapt the suggestion to @Frakcool's idea of having ***some*** columns of the table editable. Keep a `boolean[] editable;` that has the same size as the number of columns in the table. Change the `setEditable(boolean)` method to something `setEditable(boolean, int)` - the `boolean` tells whether to make the column editable, and the `int` specifies the column. Then change the `isCellEditable()` method to get the `boolean` from the array. – Andrew Thompson Jul 10 '16 at 14:45
  • @AndrewThompson I edited the question to hopefully clarify what I mean – Dan Jul 10 '16 at 15:10
  • @Frakcool I edited the question to hopefully clarify what I mean – Dan Jul 10 '16 at 15:10
  • Umm.. and what is it about my initial suggestion, or the alteration suggested by @Frakcool, that does not work for this situation. *"whether the table is editable"* you keep referring to the table as though the methodology should be attached to the table object. Forget the table itself, it is the model that is the key class here. – Andrew Thompson Jul 10 '16 at 15:22

1 Answers1

2

things such as whether the table is editable or not as there are no methods which support toggling whether the table is editable after the model has been made

You will need to provide a custom TableModel. You can build this functionality into the DefaultTableModel by extending the model and adding a couple of methods.

Or you can check out the Row Table Model.

Among other features it allows you to make the entire model editable or not. You can also control whether individual columns are editable or not.

camickr
  • 321,443
  • 19
  • 166
  • 288