julliiie
julliiie

Reputation: 9

Delete the number of columns of a JTable

Do you know if it's possible to edit a Jtable which has 6 columns ?

column = jTableMus.getColumnModel().getColumn(0);
        column.setPreferredWidth(50);
        column = jTableMus.getColumnModel().getColumn(1);
        column.setPreferredWidth(150);
        column = jTableMus.getColumnModel().getColumn(2);
        column.setPreferredWidth(100);
        column = jTableMus.getColumnModel().getColumn(3);
        column.setPreferredWidth(80);
        column = jTableMus.getColumnModel().getColumn(4);
        column.setPreferredWidth(50);
        column = jTableMus.getColumnModel().getColumn(5);

In fact, I would like to know how to change on 4 columns instead of 6 columns.

Thank for your answers.

Upvotes: 0

Views: 44

Answers (1)

camickr
camickr

Reputation: 324078

If you want to control which columns are editable you can override the isCellEditable(...) method of the TableModel.

So to make only the first 4 columns editable you could do something like:

@Override
public Boolean isCellEditable(int row, int column)
{
    return (column < 4) ? true : false;
}

Edit:

I would like to delete 2 columns

A couple of different ways. You can delete the columns using:

  1. getColumn(...) and removeColumn(...) methods from JTable API.
  2. getColumn(...) and removeColumn(...) methods from TableColumnModel API

The approach you use will depend on whether you want to remove a column based on its index or its column name.

Upvotes: 1

Related Questions