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

Asked

Viewed 415 times

1

It is possible to hide and lock the last line of a DefaulTableModel?

I have this difficulty and I am not able to solve it. If it is not at least 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’d like to hide the last line.

  • Good.. It is necessary a minimum, complete and verifiable example, for a question whether it is possible to block a row from any table? if the table has 5 lines, is it possible to block the last? sorry for the question, it is my first topic.

  • And where is your tablemodel? Which line needs to be blocked? When it needs to be blocked?

  • put an example of tablemodel, I need the last line to be hidden as soon as I open the frame, I just need it to be hidden

  • I updated the answer.

1 answer

3


If by "block" you mean prevent the line data from being edited, simply overwrite the method 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 (crossing row x column) of the table, stating whether or not the table’s data can be edited. The above logic will return true while the line’s Dice is smaller than the last line.

But it doesn’t make much sense to block a line if you don’t even want to display it. The above method already prevents it from being changed by the user.

The example presented is also very generic, it does not present a problem to be solved. If you want to hide a row, the least complicated way is to remove it in the table with the method removeRow(), passing your index.


If you still want to insist on hiding the line while already blocked, is possible if the table already has a Tablerowsorter and you create a RowFilter that shows the rows to be displayed in the table, so that when it is the last one, it does 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 method include returns a boolean if a given entry can be displayed in the table, in this case I am returns true for all cases where the input from the TableModel is smaller than the last line. It continues to exist 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 rows of tables.

  • I thank you for your reply, I am trying to get around a situation where I need to add an empty line and for q it n get visible I wanted to hide it

  • @Lucas16 no solution I suggest based on this generic code will help you. If you follow the answer code logic, regardless of what the last line is, it will never appear. What if the line to be hidden at some point ceases to be the last? You can’t suggest anything without having a real representation of your problem.

  • Yes, but I needed to hide it, simply hide it in jtable, would you give me an example to hide it?

  • @Lucas16 is just you override the getValueAt method of defaulTableModel, checking if the current line is the last, with the same logic I used in the answer.

  • It would be a bother if you give me this example? maybe it will be useful for other people too. I got a little confused.

  • @Lucas16 is already in the answer. Just add model.removeRow(totalDeLinhas-1). Only it will permanently remove that line.

  • but I want to hide the line, not remove it kkkkkk....

  • @Lucas16 the suggested lock and hide solution did not serve? If yes, you can mark the question as completed by clicking on v next to the answer.

  • I’m sorry, I was away and I forgot when I came back, sorry man. It worked super well!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.