Add line in Jtable at the click of the button

Asked

Viewed 2,074 times

3

I’m starting in creating interface in Java, an example I’m trying to do is to have a form with textField nome and email, and by clicking on the Enviar the data of those textField goes to my JTable, but I’m unable to manipulate my table, follow the code of the last attempt:

import java.awt.GridLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/** * @author Rosicléia Frasson */
public class teste01 extends JFrame {

    JPanel painelFundo;
    JTable tabela; // minha tabela
    private JTextField nome;
    private JLabel txtnome;
    private JLabel txtemail;
    private JTextField email;
    private JButton enviar;
    private JButton cancelar;

    DefaultTableModel modelo = (DefaultTableModel) tabela.getModel(); //pega modelo da tabela

    private int dado = 0;

    public teste01() {



        enviar.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(!nome.getText().equals("") && !email.getText().equals(""))
                {
                    nome.setText("stack"); // apenas para ver se entrou na condição

                    modelo.addColumn("Nome"); // era para adicionar as colunas
                    modelo.addColumn("Email");

                   /* dados[dado][0] = nome.getText();
                    dados[dado][1] = email.getText();
                    dado += 1;
                    tabela = new JTable(dados, colunas);
                    tabela.repaint();*/
                }
            }
        });
        cancelar.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                nome.setText("");
                email.setText("");
            }
        });
        painelFundo.addComponentListener(new ComponentAdapter() {
        });
    }

    // cria minha interface
    public static void main(String[] args) {
        JFrame frame = new JFrame("teste01");
        frame.setContentPane(new teste01().painelFundo);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
  • Have you thought about creating your own Tablemodel? I’m working on an answer to that in another question, it might help. Defaultablemodel is such a mess, besides slowing down the code.

  • Before I tried implementing one myself by looking at some examples. But it didn’t work, the table on the interface is blank. Even columns are not added.

  • As soon as I can answer you, but I would need to know if name and email is part of some type of entity, or object, for example: personal. Having an object as focus, it is easier to create a tablemodel. I have some templates ready until, just modify them.

  • email and nome are textField of the same class(inteface), do not come from any external entity or object. As I mentioned in the question, it is a simple example, a single class that has on the interface two textField(name and email), two buttons (send, cancel) and a Ftable (table). The goal is only to click Submit, take the name and email of the textField and place in the table as a new row.

2 answers

0


How are you using DefaulTableModel, just call the method addRow(), passing a vector of Strings with the value of the two fields:

String[] novaLinha = {nome.getText(), email.getText()};
modelo.addRow(novaLinha);

The very one DefaultTableModel will be in charge of notifying listeners and redesigning their table with the new line.

  • I had tried so and did not give.

  • @lvcs this method is perfectly functional, I even created an example here to test that it would actually work before posting. Maybe the problem is elsewhere, like for example, are you adding the table in a Jscrollpane?? Add some prints of the problem to see if you can identify it.

  • I tried with and without the table inside Jscrollpane, I am following these steps: Declassified a new Defaulttablemodel, If the table model as the new Defaulttablemodel, I add a new line in the Model, and nothing happens, it is not added, no error is shown even in the console. .

  • The code is practically the one described in the question. But no row or column appears in the table, whether or not it is inside Jscrollpane

  • @lvcs posts the screen code where you build your table completely in question, the problem must be elsewhere in the code. With it complete, it is easier to analyze.

0

I’ve been trying the same thing and in the action of the button to add put the following code.

DefaultTableModel nada = (DefaultTableModel) jTable1.getModel();

 nada.addRow(new Object[]{null,null});

Browser other questions tagged

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