How to hide and block the last line of a JTable?

1

Is it possible to hide and block the last line of a DefaulTableModel ?

I have this difficulty and I can not solve it. If not, is it possible to hide this line?

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Main extends JFrame {
  DefaultTableModel model = new DefaultTableModel(new Object[][] {
       {"Jack","19","Masculino"}, {"Eddie","56","Masculino"}, {"Gina","34","Feminino"},
      {"Klaus","18","Masculino"}, {"Erika","20","Feminino"},  {"Roberto","29","Masculino"},{"Maria","30","Feminino"} },
      new Object[] { "Nome:", "Idade:", "Sexo:" });

  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTable table = new JTable(model);
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    pack();
  }

  public static void main(String arg[]) {
    new Main().setVisible(true);
  }
}

I would like to hide the last line.

    
asked by anonymous 05.10.2017 / 16:02

1 answer

3

If you "block" do not want the row data to be edited, just override the isCellEditable of TableModel as follows:

DefaultTableModel model =  new DefaultTableModel(dados, colunas) {

    @Override
    public boolean isCellEditable(int row, int column) {
        return row < (getRowCount() - 1);
    }
}

table=new JTable(model);

This method returns a boolean for each cell (line x row column) of the table, indicating whether its data can be edited or not. The above logic will return true while the line index is less than the last line.

But it does not make much sense to block a line if you do not want to display it. The above method already prevents it from being changed by the user.

The example shown is also very generic, does not really present a problem to be solved. If you want to hide a line, the least complicated way is to remove it in the table with the removeRow() ", passing its index.

If you still want to hide the line already blocked, is possible if the table already has a TableRowSorter and you create a RowFilter filter the rows to be displayed in the table, so that when it is the last, do not display:

RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>(){

    @Override
    public boolean include(javax.swing.RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
        int modelRow = entry.getIdentifier();
        return modelRow < (table.getRowCount() - 1);
    }
};

...

((TableRowSorter<?>)table.getRowSorter()).setRowFilter(filter);

The include method returns a boolean if a given entry can be displayed in the table, in this case, I am returning true for all cases where the index of the TableModel line is less than the last row's. It still exists in the model, but is not displayed in the view.

In the Oracle tutorial on JTable you can read more about how to create and apply filters and sort table rows.

    
05.10.2017 / 16:11