preciso implementar que a Jlist da classe ListCad receba os dados salvos na classe Cliente
atraves do botão salvar da classe InsCad.
segue codigo para possivel analise. nao consigo importar a JList list do metodo public ListCad() para a classe InsCad.
codigo Inscad
package Tabela;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.print.DocFlavor.INPUT_STREAM;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.UIManager;
import javax.swing.ListModel;
public class InsCad extends JFrame {
ListCad lista = new ListCad();
DefaultListModel model = new DefaultListModel();
private JPanel contentPane;
private JTextField txtNome;
private JTextField txtCod;
private JTextField txtEmail;
private JTextField txtTel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InsCad frame = new InsCad();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public InsCad() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txtNome = new JTextField();
txtNome.setBounds(114, 60, 175, 20);
contentPane.add(txtNome);
txtNome.setColumns(10);
JLabel lblNewLabel = new JLabel("Nome");
lblNewLabel.setBounds(34, 66, 46, 14);
contentPane.add(lblNewLabel);
JLabel lblCodigo = new JLabel("Codigo");
lblCodigo.setBounds(34, 35, 46, 14);
contentPane.add(lblCodigo);
txtCod = new JTextField();
txtCod.setColumns(10);
txtCod.setBounds(114, 29, 70, 20);
contentPane.add(txtCod);
JLabel lblNewLabel_1_1 = new JLabel("Email");
lblNewLabel_1_1.setBounds(34, 96, 46, 14);
contentPane.add(lblNewLabel_1_1);
txtEmail = new JTextField();
txtEmail.setColumns(10);
txtEmail.setBounds(114, 90, 175, 20);
contentPane.add(txtEmail);
JLabel lblNewLabel_1_1_1 = new JLabel("Telefone");
lblNewLabel_1_1_1.setBounds(34, 127, 70, 14);
contentPane.add(lblNewLabel_1_1_1);
txtTel = new JTextField();
txtTel.setColumns(10);
txtTel.setBounds(140, 121, 105, 20);
contentPane.add(txtTel);
JButton btnSalvar = new JButton("Salvar");
btnSalvar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Cliente cliente = new Cliente();
int cod = Integer.parseInt(txtCod.getText());
cliente.setCodigo(cod);
txtCod.setText("");
cliente.setNome(txtNome.getText());
txtNome.setText("");
cliente.setEmail(txtEmail.getText());
txtEmail.setText("");
cliente.setTelefone(txtTel.getText());
txtTel.setText("");
List <Cliente> clientes = new ArrayList<Cliente>();
clientes.add(cliente);
for (Cliente c : clientes) {
System.out.println(c.getCodigo()+" ,"+c.getNome()+" ,"+c.getEmail()+" ,"+c.getTelefone());
lista.model.addElement(cliente.getCodigo()+cliente.getNome()+cliente.getEmail()+cliente.getTelefone());
}
}
});
btnSalvar.setBounds(34, 201, 89, 23);
contentPane.add(btnSalvar);
JButton btnVoltar = new JButton("Voltar");
btnVoltar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Tela t = new Tela ();
t.setVisible(true);
}
});
btnVoltar.setBounds(300, 201, 89, 23);
contentPane.add(btnVoltar);
}
}
codigo cliente
package Tabela;
import java.util.ArrayList;
public class Cliente {
private int codigo;
private String nome;
private String email;
private String telefone;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public int getCodigo() {
return codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
}
codigo ListCad
package Tabela;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ListCad extends JFrame {
DefaultListModel model = new DefaultListModel();
Cliente cliente = new Cliente();
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ListCad frame = new ListCad();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ListCad() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JList list = new JList();
list.setBounds(10, 11, 414, 211);
contentPane.add(list);
model.addElement(cliente.getCodigo()+cliente.getNome()+cliente.getEmail()+cliente.getTelefone());
list.setModel(model);
JButton btnNewButton = new JButton("Voltar");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Tela tela = new Tela();
tela.setVisible(true);
}
});
btnNewButton.setBounds(335, 233, 89, 23);
contentPane.add(btnNewButton);
}
}