Problems with paging on Jtable

Asked

Viewed 277 times

0

I am creating an application where I fill the data of a table through the database, but there are many so I wanted to organize better.

However, the results shown in the table are not constant. The implemented methods behave in different ways even with the same button being pressed.

When I use the alunos_1.json, that has a page with less than the maximum number of records allowed, the table loads and everything works the way I said, but when I load a json alunos.json that has a number divisible by the maximum number of records does not work.

Table class

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Teste extends JFrame{
   //MAIN METHOD
  public static void main(String[] args)
  {

       EventQueue.invokeLater(new Runnable()
       {
           public void run()
           {
               //INITIALIZE JFRAME FORM
               teste.Teste form=new teste.Teste();
               form.setVisible(true);;
           }
       });

  }

  TesteTableModel tableModel = new TesteTableModel();
  List<TesteModel> resultado = new ArrayList<TesteModel>();
  List<TesteModel> lista = new ArrayList<TesteModel>();
  int indiceLista, maxLista, resto, totalPag = 0;
  int paginaAtual = 1;

  private static final int ITENS_POR_PAG = 5;

  private java.awt.Button btnRemover, btnProxima, btnAnterior, btnPrimeira, btnUltima;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JScrollPane jScrollPane;
  private javax.swing.JTable jTable;
  private java.awt.ScrollPane scrollPane;


  //CONSTRUCTOR
  public Teste()
  {
        scrollPane = new java.awt.ScrollPane();
        jScrollPane = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        btnRemover = new java.awt.Button();
        btnAnterior = new java.awt.Button();
        btnProxima = new java.awt.Button();
        btnUltima = new java.awt.Button();
        btnPrimeira = new java.awt.Button();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(500, 400));

        jTable.setModel(tableModel);

        jScrollPane.setViewportView(jTable);

        scrollPane.add(jScrollPane);

        btnRemover.setLabel("Remover");

        btnAnterior.setLabel("<");

        btnProxima.setLabel(">");

        btnUltima.setLabel(">>");

        btnPrimeira.setLabel("<<");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(btnRemover, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 293, Short.MAX_VALUE)
                .addComponent(btnPrimeira, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnProxima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(btnUltima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnRemover, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnProxima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnUltima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPrimeira, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        btnRemover.getAccessibleContext().setAccessibleName("btnRemover");

        pack();

        btnRemover.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                tableModel.deletarLinhas();
            }
        });

        btnProxima.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(indiceLista <= lista.size()){
                    btnAnterior.setEnabled(true);
                    btnPrimeira.setEnabled(true);
                    resultado = carregaProximaPagina(lista);

                    for(TesteModel teste: resultado){
                        tableModel.addRow(teste);
                    }
                }
            }
        });

        btnAnterior.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0){
                if(indiceLista != 0){
                    btnProxima.setEnabled(true);
                    btnUltima.setEnabled(true);
                    resultado = carregaPaginaAnterior(lista);

                    for(TesteModel teste: resultado){
                        tableModel.addRow(teste);
                    }
                }
            }
        });

        btnUltima.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                resultado = carregaUltimaPagina(lista);

                for(TesteModel teste: resultado){
                    tableModel.addRow(teste);
                }
            }
        });

        btnPrimeira.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                resultado = carregaPrimeiraPagina(lista);

                for(TesteModel teste: resultado){
                    tableModel.addRow(teste);
                }
            }
        });

         //Preenche dados iniciais da tabela
        lista = tableModel.lerJSON();

        paginaAtual = (int) lista.size() / ITENS_POR_PAG;
        resto = lista.size() % ITENS_POR_PAG;

        System.out.println("\npagina:" + paginaAtual
                + "\nresto:" + resto);

        resultado = carregaProximaPagina(lista);

        for(TesteModel teste: resultado){
            tableModel.addRow(teste);
        }

        btnAnterior.setEnabled(false);
        btnPrimeira.setEnabled(false);

    }                       

    private void limparTabela() {  
        while (jTable.getModel().getRowCount() > 0) {  
           ((TesteTableModel) jTable.getModel()).removeRow(0);  
       } 
    } 

    public List<TesteModel> carregaProximaPagina(List<TesteModel> testes){
        limparTabela();
        List<TesteModel> resultado = new ArrayList<TesteModel>();
        if(resto > 0){
            if(paginaAtual > 0){
                indiceLista = maxLista;
                maxLista += ITENS_POR_PAG;
                for(int i = indiceLista; i < maxLista; i++){
                    resultado.add(testes.get(i));
                }
                System.out.println("\nindiceLista:" + indiceLista
                        + "\nmaxLista:" + maxLista
                        + "\npagina:" + paginaAtual);
            } else { 
                indiceLista = maxLista;
                maxLista += resto;
                for(int i = indiceLista; i < maxLista; i++){
                    resultado.add(testes.get(i));
                }
                btnProxima.setEnabled(false);
                btnUltima.setEnabled(false);
                System.out.println("\nindiceLista:" + indiceLista
                        + "\nmaxLista:" + maxLista
                        + "\npagina:" + paginaAtual);
            }
        }
        paginaAtual--;       
        return resultado;
    }

    public List<TesteModel> carregaPaginaAnterior(List<TesteModel> testes){
        limparTabela();
        List<TesteModel> resultado = new ArrayList<TesteModel>();
        if(paginaAtual == 0){
            maxLista = indiceLista;
            indiceLista = maxLista - ITENS_POR_PAG;

            for(int i = indiceLista; i < maxLista; i++){
                resultado.add(testes.get(i));
            }
            System.out.println("\nindiceLista:" + indiceLista
                    + "\nmaxLista:" + maxLista
                    + "\npagina:" + paginaAtual);
        } else {
            if(paginaAtual == ((int) testes.size() / ITENS_POR_PAG)){
                btnAnterior.setEnabled(false);
                btnPrimeira.setEnabled(false);
            }

            indiceLista -= ITENS_POR_PAG;
            maxLista -= ITENS_POR_PAG;
            for(int i = indiceLista; i < maxLista; i++){
                resultado.add(testes.get(i));
            }
            System.out.println("\nindiceLista:" + indiceLista
                    + "\nmaxLista:" + maxLista
                    + "\npagina:" + paginaAtual);
        }
        paginaAtual++;
        return resultado;
    }

    public List<TesteModel> carregaPrimeiraPagina(List<TesteModel> testes){
        limparTabela();
        List<TesteModel> resultado = new ArrayList<TesteModel>();

        totalPag = (int) lista.size() / ITENS_POR_PAG;

        indiceLista = 0;
        maxLista = ITENS_POR_PAG;
        for(int i = indiceLista; i < maxLista; i++){
            resultado.add(testes.get(i));
        }
        System.out.println("\nindiceLista:" + indiceLista
                + "\nmaxLista:" + maxLista
                + "\npagina:" + paginaAtual);

        return resultado;
    }

    public List<TesteModel> carregaUltimaPagina(List<TesteModel> testes){
        limparTabela();
        List<TesteModel> resultado = new ArrayList<TesteModel>();

        totalPag = (int) lista.size() / ITENS_POR_PAG;

        if(resto != 0) {
            indiceLista = ITENS_POR_PAG * totalPag;
            maxLista = indiceLista + resto;
            for(int i = indiceLista; i < maxLista; i++){
                resultado.add(testes.get(i));
            }
            paginaAtual = 0;
            System.out.println("\nindiceLista:" + indiceLista
                    + "\nmaxLista:" + maxLista
                    + "\npagina:" + paginaAtual);
        } else {
            indiceLista = ITENS_POR_PAG * totalPag;
            maxLista = indiceLista + ITENS_POR_PAG;
            for(int i = indiceLista; i < maxLista; i++){
                resultado.add(testes.get(i));
            }
            System.out.println("\nindiceLista:" + indiceLista
                    + "\nmaxLista:" + maxLista
                    + "\npagina:" + paginaAtual);
        }
        return resultado;
    }

}

Testetablemodel

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import jdk.nashorn.internal.parser.JSONParser;
import org.json.JSONObject;

public class TesteTableModel extends AbstractTableModel {


    private List<TesteModel> dados = new ArrayList<>();
    private String[] colunas = {"Selecionar", "Nome"};


    public Class<?> getColumnClass(int columnIndex) {
        return columnIndex == 0 ? Boolean.class : super.getColumnClass(columnIndex);
    }

    @Override
    public String getColumnName(int column){
        return colunas[column];
    }

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

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

    @Override
    public Object getValueAt(int linha, int coluna) {
        switch(coluna){
            case 0:
                return dados.get(linha).getSelecionado();
            case 1:
                return dados.get(linha).getNome();
        }

        return null;
    }

    public void setValueAt(Object valor, int linha, int coluna) {
        TesteModel tm = dados.get(linha);
        switch (coluna) {
        case 0:
            tm.setSelecionado(new Boolean((Boolean) valor));
            break;
        }
        fireTableDataChanged();
    }

    public void addRow(TesteModel tm) {
        this.dados.add(tm);
        this.fireTableDataChanged();    
    }

    public void removeRow(int linha){
        this.dados.remove(linha);
        this.fireTableRowsDeleted(linha, linha);
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 0; 
    }

    public void deletarLinhas() {

    for (int i = getRowCount() - 1; i >= 0; i--) {
        if (dados.get(i).getSelecionado()) {
        removeRow(i);
            }
    }
    }

    public String lerArquivo() {
        String linha = "";

        try {
          FileReader arq = new FileReader("C:\\Users\\maily\\Documents\\NetBeansProjects\\Teste\\src\\teste\\alunos_1.json");
          BufferedReader lerArq = new BufferedReader(arq);

          linha = lerArq.readLine(); 
          /*while (linha != null) {
            System.out.printf(linha);
            linha = lerArq.readLine(); // lê da segunda até a última linha
          }*/
          arq.close();

        } catch (IOException e) {
            System.err.printf("Erro na abertura do arquivo: %s.\n",
              e.getMessage());
        }
        //System.out.println(linha);
        return linha;
    }

    public List<TesteModel> lerJSON() {
        String str = lerArquivo();
        Type type = new TypeToken<List<TesteModel>>(){}.getType();

        Gson gson = new GsonBuilder().create();
        List<TesteModel> lista = gson.fromJson(str, type);

        for(TesteModel teste: lista){
            System.out.println(teste.getSelecionado());
            System.out.println(teste.getNome());
        }
        return lista;
    }

}

Model class

public class TesteModel {
    private Boolean select;
    private String name;

    public Boolean getSelecionado() {
        return select;
    }

    public void setSelecionado(Boolean selecionado) {
        this.select = selecionado;
    }

    public String getNome() {
        return name;
    }

    public void setNome(String nome) {
        this.name = nome;
    }

}

json students.

[{"select": "false", "name": "Ana 01"}, {"select": "false", "name": "João"}, {"select": "false", "name": "Laura"}, {"select": "false", "name": "Bruno"}, {"select": "false", "name": "Carina"}, {"select": "false", "name": "José 02"}, {"select": "false", "name": "Maria"}, {"select": "false", "name": "Renato"}, {"select": "false", "name": "Paula"}, {"select": "false", "name": "Juliano"}, {"select": "false", "name": "Júlio 03"}, {"select": "false", "name": "Amanda"}, {"select": "false", "name": "André"}, {"select": "false", "name": "Tales"},{"select": "false", "name": "Pedro"}, {"select": "false", "name": "João 04"}, {"select": "false", "name": "Laura"}, {"select": "false", "name": "Bruno"}, {"select": "false", "name": "Carina"},{"select": "false", "name": "Pedro"}, {"select": "false", "name": "João 05"}, {"select": "false", "name": "Laura"}, {"select": "false", "name": "Bruno"}, {"select": "false", "name": "Carina"},{"select": "false", "name": "Pedro"}, {"select": "false", "name": "João 06"}, {"select": "false", "name": "Laura"}, {"select": "false", "name": "Bruno"}, {"select": "false", "name": "Carina"},{"select": "false", "name": "Pedro"}]

alunos_1.json

[{"select": "false", "name": "Ana 01"}, {"select": "false", "name": "João"}, {"select": "false", "name": "Laura"}, {"select": "false", "name": "Bruno"}, {"select": "false", "name": "Carina"}, {"select": "false", "name": "José 02"}, {"select": "false", "name": "Maria"}, {"select": "false", "name": "Renato"}, {"select": "false", "name": "Paula"}, {"select": "false", "name": "Juliano"}, {"select": "false", "name": "Júlio 03"}, {"select": "false", "name": "Amanda"}, {"select": "false", "name": "André"}, {"select": "false", "name": "Tales"},{"select": "false", "name": "Pedro"}, {"select": "false", "name": "João 04"}, {"select": "false", "name": "Laura"}, {"select": "false", "name": "Bruno"}, {"select": "false", "name": "Carina"},{"select": "false", "name": "Pedro"}, {"select": "false", "name": "João 05"}, {"select": "false", "name": "Laura"}, {"select": "false", "name": "Bruno"}, {"select": "false", "name": "Carina"},{"select": "false", "name": "Pedro"}, {"select": "false", "name": "João 06"}, {"select": "false", "name": "Laura"}, {"select": "false", "name": "Bruno"}, {"select": "false", "name": "Carina"},{"select": "false", "name": "Pedro"}, {"select": "false", "name": "Bruno"}, {"select": "false", "name": "Carina"},{"select": "false", "name": "Joselino RESTO"}]

Gson’s lib may be lowland here.

  • 1

    @Lys is a lot for an objective answer, I would need a tutorial (that does not fit in the proposal of this site here). The ideal would be you at least [Edit] and give more details of what you have, the structures, so that we can help. Help us to help you, and we will guide you through here until you get a possible question to answer.

  • For example, you can post a basic code without the paging part, but with the structures you already have ready, simplifying a little. When you learn with little data, you will surely know with more data.

  • It depends on how you load the data. If you bring all the data from the table at once, I think it’s a waste of time making pagination, not worth the effort because it will be just visual, the performance and memory cost will be the same.

  • In the final application, data will be loaded from a JSON.

  • @Lys edited the comment.

  • I will bring at once yes reading from JSON, but it is a requirement that this table has pagination.

  • @Lys vejo q vc is still using absolute layout and due to this, it is not possible to make paging efficiently, because of the line size. I suggest you read my tip of your last reply and switch to relative layouts, so it’s easy to implement

  • Or can I suggest a General solution and you look and study how to adapt to your code.

  • Why use joptionpane to show table? The purpose of this component is not well for this.

  • I’m sorry, but I’m not using Joptionpane anywhere. I’m trying to recreate with some relative layout so that I can get the help I need.

  • Sorry, I mistook it for an old code here. Just one last doubt, Voce will rescue all table data in one time, but wants to display them by paging?

  • I need them to appear for example, 10 records at a time. So, I think it’s smarter to slowly rescue and store the index that I stopped to continue after.

  • Then you would need to present a [mcve] how Voce retrieves data from its actual table, as the example will be based on the data already in the table. Without seeing and testing the actual implementation Voce recovers the data has no way to suggest anything that will suit you.

Show 9 more comments

1 answer

1


It is possible to do this by applying the approach of Soen’s reply, where we only limit the amount of data to be displayed on the screen by removing the scroll bar and using the buttons to move it manually.

The key here is to use the height of the table rows times the amount you want to display to move the scroll bar, so in the system inside the buttons btnPrevious and btnNext I calculate the height of the next set of lines. In the adapted example of your code, the bar will vertically move exactly the height of 5 rows of the table at a time, to its end or its incision.

For the buttons that move to the beginning and end of the table(btnFirst and btnLast), i just set the minimum and maximum values of the bar, causing it to scroll to the beginning or end of the table.

The screen with the table and buttons was like this:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class JTablePaginada extends JFrame {

    private JPanel contentPane, buttonPane;
    private JButton btnFirst, btnPrevious, btnNext, btnLast, btnClean;
    private JTable table;
    private TesteTableModel model;
    
    private static final int ITENS_POR_PAG = 5;
    

    public static void main(String[] args) {
        EventQueue.invokeLater(()-> new JTablePaginada().setVisible(true));
    }


    public JTablePaginada() {
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(500, 400));
        contentPane = new JPanel();
        
        model = new TesteTableModel(JSONUtils.JSONtoList());
        
        table = new JTable(model);
        
        JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        //o width é um pequeno "workaround pra tabela ficar certinha na tela
        //a altura é definida pela quantidade de itens que quer exibir 
        //mais a altura do cabeçalho
        scrollPane.setPreferredSize(new Dimension(getPreferredSize().width - 20, table.getRowHeight() * ITENS_POR_PAG + table.getTableHeader().getPreferredSize().height));

        contentPane.add(scrollPane);
        this.add(contentPane, BorderLayout.CENTER);
        
        btnClean = new JButton("Remover tudo");
        btnFirst = new JButton("<<");
        btnFirst.addActionListener(e -> {
            JScrollBar bar = scrollPane.getVerticalScrollBar();
            bar.setValue(0);
        });
        
        btnPrevious = new JButton("<");
        btnPrevious.addActionListener(e -> {
            int height = table.getRowHeight() * (ITENS_POR_PAG - 1);
            JScrollBar bar = scrollPane.getVerticalScrollBar();
            bar.setValue(bar.getValue() - height);
        });
        
        btnNext = new JButton(">");
        btnNext.addActionListener(e -> {
            int height = table.getRowHeight() * (ITENS_POR_PAG - 1);
            JScrollBar bar = scrollPane.getVerticalScrollBar();
            bar.setValue(bar.getValue() + height);
        });
        
        btnLast = new JButton(">>");
        btnLast.addActionListener(e -> {
            JScrollBar bar = scrollPane.getVerticalScrollBar();
            bar.setValue(bar.getMaximum());
        });
        
        buttonPane = new JPanel();
        buttonPane.add(btnFirst);
        buttonPane.add(btnPrevious);
        buttonPane.add(btnNext);
        buttonPane.add(btnLast);
        
        this.add(buttonPane, BorderLayout.SOUTH);        
        pack();
    }
}

Running with the file alunos.json:

inserir a descrição da imagem aqui

Remembering that no matter how much data here, we are just moving the scroll bar through the table rows, without displaying it on the screen.


Supplementary comments

I’ve made other code modifications that are important to comment on, although I run away from the problem, which has already been solved above, but can affect new implementations, including the paging itself:

  • your class TableModel should not read the json file nor convert it, it hurts the principle of class cohesion, beyond the concept of sole responsibility, where the class must do only that for which it has been designated. So I moved the two methods of reading the file and converting json to List for a new Jsonutils class:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class JSONUtils {

    private static String strjson = null;

    
    //isso é pra evitar instanciacao da classe
    //ja que os métodos sao estaticos
    private JSONUtils() {

        if (strjson == null)
            strjson = lerArquivo();
    }

    public static List<TesteModel> JSONtoList() {
        String str = lerArquivo();
        Type type = new TypeToken<List<TesteModel>>() {
        }.getType();

        Gson gson = new GsonBuilder().create();
        List<TesteModel> lista = gson.fromJson(str, type);

        for (TesteModel teste : lista) {
            System.out.println(teste.getSelecionado());
            System.out.println(teste.getNome());
        }
        return lista;
    }

    private static String lerArquivo() {
        String linha = "";

        try {
            FileReader arq = new FileReader("C:\\temp\\alunos.json");
            BufferedReader lerArq = new BufferedReader(arq);

            linha = lerArq.readLine();
            /*
             * while (linha != null) { System.out.printf(linha); linha = lerArq.readLine();
             * // lê da segunda até a última linha }
             */
            arq.close();

        } catch (IOException e) {
            System.err.printf("Erro na abertura do arquivo: %s.\n", e.getMessage());
        }
        // System.out.println(linha);
        return linha;
    }
}
  • in class TesteTableModel, the method deletarLinhas() can be simplified. You do not need to scan item by item from a list to delete everything, the class List has already natively a method to clear the list, which is the clear. That’s why it’s important to ALWAYS read the class documentation, that’s how you learn how to function and avoid creating unnecessary code for tasks that java already has. Just use the quoted method and notify the table:
public void deletarLinhas() {
    this.dados.clear();
    this.fireTableDataChanged();
}
  • still in class TesteTableModel, I suggest creating a builder as below, bearing in mind what you said in chat regarding the reuse of the table for other json files. This way you can create some button or function that imports a new file and populate it in the table, just create a new TableModel:
//flexibilizando o tablemodel
public TesteTableModel(List<TesteModel> model) {
    this.dados = model;
}

I reinforce the tip I gave in chat: always try to understand the basics of how a component works, java-swing is not such an easy-to-handle API, although it is not as complex, but it is essential that you try to understand how the component works, what its functionalities, if not every code you find out and try to adapt it to yours, will only create a cascade of problems.

  • 1

    Thanks again, your answer was perfect. The solution is actually much simpler to both implement and understand. Thank you so much for not only helping me with the problem but for helping me improve the code in general with your tips. Were very important!

Browser other questions tagged

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