I'm with dificutly for receiving values from one JFrame and insert in the JTable.
I want know how to receive one object academico ready to be inserted in my JTable. From de my class FrameTemp.
Class model Acadêmico.
package model;
public class Academico {
private String matricula;
private String nome;
private String curso;
private int periodo;
public Academico() {
}
public Academico(String matricula, String nome, String curso, int periodo) {
this.matricula = matricula;
this.nome = nome;
this.curso = curso;
this.periodo = periodo;
}
public String getMatricula() {
return matricula;
}
public void setMatricula(String matricula) {
this.matricula = matricula;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCurso() {
return curso;
}
public void setCurso(String curso) {
this.curso = curso;
}
public int getPeriodo() {
return periodo;
}
public void setPeriodo(int periodo) {
this.periodo = periodo;
}
}
Class Frame that should receive values.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
public class Frame extends JFrame implements ActionListener {
private static Academico novoAcademico;
private static FrameTemp frameTemporario;
private JTable tabela;
private DefaultTableModel modelo;
private ArrayList<Academico> academicos;
private JButton btAdd, btAtualizar, btDelete;
private JPanel painel, painelGrid;
private BorderLayout borderLayout;
private GridLayout gridLayout;
public Frame(Academico academico) {
this.setTitle("Acadêmicos Cadastrados");
this.painel = new JPanel();
this.painelGrid = new JPanel();
borderLayout = new BorderLayout();
gridLayout = new GridLayout(1, 3);
setContentPane(painel);
this.setLayout(borderLayout);
painel.add(painelGrid);
painelGrid.setLayout(gridLayout);
this.modelo = new DefaultTableModel();
this.tabela = new JTable(modelo);
this.btAdd = new JButton("Insert");
this.btAdd.setFont(new Font("Verdana", Font.PLAIN, 20));
this.btAdd.setBackground(Color.BLACK);
this.btAdd.setForeground(Color.WHITE);
this.btAtualizar = new JButton("Update");
this.btAtualizar.setFont(new Font("Verdana", Font.PLAIN, 20));
this.btAtualizar.setBackground(Color.BLACK);
this.btAtualizar.setForeground(Color.WHITE);
this.btDelete = new JButton("Delete");
this.btDelete.setFont(new Font("Verdana", Font.PLAIN, 20));
this.btDelete.setBackground(Color.BLACK);
this.btDelete.setForeground(Color.WHITE);
this.modelo.addColumn("Matrícula");
this.modelo.addColumn("Nome");
this.modelo.addColumn("Curso");
this.modelo.addColumn("Período");
this.tabela.getColumnModel().getColumn(0).setPreferredWidth(20);
this.tabela.getColumnModel().getColumn(1).setPreferredWidth(30);
this.tabela.getColumnModel().getColumn(2).setPreferredWidth(50);
this.tabela.getColumnModel().getColumn(3).setPreferredWidth(20);
this.academicos = new ArrayList<>();
this.academicos.add(new Academico("123", "Student1", "Information System", 3));
this.academicos.add(new Academico("234", "Student2", "Science", 5));
this.academicos.add(new Academico("345", "Student3", "Engineer", 7));
for (Academico a : this.academicos) {
this.modelo.addRow(new Object[]{a.getMatricula(), a.getNome(), a.getCurso(), a.getPeriodo()});
}
this.painel.add(BorderLayout.NORTH, new JScrollPane(this.tabela));
this.painel.add(BorderLayout.SOUTH, painelGrid);
this.painelGrid.add(btAdd);
this.painelGrid.add(btAtualizar);
this.painelGrid.add(btDelete);
this.btDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int dialog = JOptionPane.YES_NO_OPTION;
int selRow = tabela.getSelectedRow();
if (selRow != -1) {
int resultado = JOptionPane.showConfirmDialog(null, "Deseja realmente excluir essa linha?", "Confirmação", dialog);
if (resultado == 0) {
modelo.removeRow(selRow);
}
}
}
}
);
this.btAdd.addActionListener(this);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Frame(novoAcademico);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btAdd) {
Academico academico = new Academico();
frameTemporario = new FrameTemp();
frameTemporario.setVisible(true);
ArrayList<Academico> lista = new ArrayList<>();
lista.add(new Academico(academico.getMatricula(), academico.getNome(), academico.getCurso(), academico.getPeriodo()));
for (Academico a : lista) {
this.modelo.addRow(new Object[]{a.getMatricula(), a.getNome(), a.getCurso(), a.getPeriodo()});
}
}
}
}
Class that should send the values
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.swingx.prompt.PromptSupport;
public class FrameTemp extends JFrame implements ActionListener {
private String title = "Adicionar novo";
private Frame frame;
private ArrayList<Academico> academicos;
private Academico academico;
private JTextField tfMatricula, tfNome, tfCurso, tfPeriodo;
private JButton btConfirmar;
private JPanel painel, painelGrid;
private BorderLayout borderLayout;
private GridLayout gridLayout;
public FrameTemp() {
this.setTitle(title);
Container c = getContentPane();
c.setLayout(new BorderLayout());
Container c2 = new JPanel();
c2.setLayout(new GridLayout(1, 4));
academico = new Academico();
academicos = new ArrayList<Academico>();
this.btConfirmar = new JButton("Confirmar");
this.tfMatricula = new JTextField();
this.tfMatricula.setToolTipText("Insira sua matrícula aqui");
PromptSupport.setPrompt("Matrícula", tfMatricula);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, tfMatricula);
PromptSupport.setFontStyle(Font.BOLD, tfMatricula);
this.tfNome = new JTextField();
this.tfNome.setToolTipText("Insira seu nome aqui");
PromptSupport.setPrompt("Nome", tfNome);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, tfNome);
PromptSupport.setFontStyle(Font.BOLD, tfNome);
this.tfCurso = new JTextField();
this.tfCurso.setToolTipText("Insira seu curso aqui");
PromptSupport.setPrompt("Curso", tfCurso);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, tfCurso);
PromptSupport.setFontStyle(Font.BOLD, tfCurso);
this.tfPeriodo = new JTextField();
this.tfPeriodo.setToolTipText("Insira seu período aqui");
PromptSupport.setPrompt("Período", tfPeriodo);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, tfPeriodo);
PromptSupport.setFontStyle(Font.BOLD, tfPeriodo);
btConfirmar.addActionListener(this);
c2.add(tfMatricula);
c2.add(tfNome);
c2.add(tfCurso);
c2.add(tfPeriodo);
c.add(btConfirmar);
c.add(BorderLayout.NORTH, c2);
c.add(BorderLayout.SOUTH, btConfirmar);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
this.academico.setMatricula(tfMatricula.getText().toString());
this.academico.setNome(tfNome.getText());
this.academico.setCurso(tfCurso.getText());
this.academico.setPeriodo(Integer.parseInt(tfPeriodo.getText()));
Frame frame = new Frame(academico);
this.dispose();
}
}