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.
– user28595
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.
– Leonardo
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.
– user28595
emailandnomeare 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.– Leonardo