A parte gráfica já está toda pronta. Agora quero configurar os códigos para que o botão “adcionar” da TelaAdcionar.java realmente adcione itens na tabela da TelaPrincipal.java. Me ajudem por favor.
Código TelaAdcionar.java:
package telas;
import javax.swing.table.DefaultTableModel;
public class TelaAdcionar extends javax.swing.JFrame {
/**
* Creates new form TelaAdcionar
private
*/
private TelaPrincipal telaPrincipal;
public TelaAdcionar(TelaPrincipal telaPrincipal) {
this.telaPrincipal = telaPrincipal;
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFormattedTextField2 = new javax.swing.JFormattedTextField();
jLabel1 = new javax.swing.JLabel();
jFormattedTextField1 = new javax.swing.JFormattedTextField();
jLabel2 = new javax.swing.JLabel();
jFormattedTextField3 = new javax.swing.JFormattedTextField();
jLabel3 = new javax.swing.JLabel();
jFormattedTextField4 = new javax.swing.JFormattedTextField();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jFormattedTextField5 = new javax.swing.JFormattedTextField();
jFormattedTextField6 = new javax.swing.JFormattedTextField();
jFormattedTextField7 = new javax.swing.JFormattedTextField();
btnLimpar = new javax.swing.JButton();
btnAdcionar = new javax.swing.JButton();
jFormattedTextField2.setText("jFormattedTextField2");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Tela de Inserir");
jLabel1.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N
jLabel1.setText("Marca:");
jFormattedTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFormattedTextField1ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N
jLabel2.setText("Modelo:");
jLabel3.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N
jLabel3.setText("Cor:");
jLabel4.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N
jLabel4.setText("Ano:");
jLabel5.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N
jLabel5.setText("Quilometragem: ");
jLabel6.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N
jLabel6.setText("Preço:");
btnLimpar.setText("Limpar");
btnAdcionar.setText("Adcionar");
btnAdcionar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAdcionarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jFormattedTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
.addComponent(jFormattedTextField1)
.addComponent(jFormattedTextField3)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jFormattedTextField7, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jFormattedTextField6)
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jFormattedTextField5))
.addGap(38, 38, 38))))
.addGroup(layout.createSequentialGroup()
.addGap(100, 100, 100)
.addComponent(btnLimpar)
.addGap(18, 18, 18)
.addComponent(btnAdcionar)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jFormattedTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
.addComponent(jFormattedTextField5))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jFormattedTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
.addComponent(jFormattedTextField7))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jFormattedTextField6, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
.addComponent(jFormattedTextField4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnLimpar)
.addComponent(btnAdcionar)))
);
pack();
}// </editor-fold>
private void jFormattedTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnAdcionarActionPerformed(java.awt.event.ActionEvent evt) {
String marca = jFormattedTextField1.getText();
String modelo = jFormattedTextField3.getText();
String cor = jFormattedTextField4.getText();
String ano = jFormattedTextField5.getText();
String quilometragem = jFormattedTextField7.getText();
String preco = jFormattedTextField6.getText();
jFormattedTextField1.setText("");
jFormattedTextField5.setText("");
jFormattedTextField3.setText("");
jFormattedTextField4.setText("");
jFormattedTextField7.setText("");
jFormattedTextField6.setText("");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TelaAdcionar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaAdcionar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaAdcionar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaAdcionar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
TelaPrincipal telaPrincipal = new TelaPrincipal(); // Verifique se TelaPrincipal possui um construtor padrão
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TelaAdcionar(telaPrincipal).setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdcionar;
private javax.swing.JButton btnLimpar;
private javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JFormattedTextField jFormattedTextField2;
private javax.swing.JFormattedTextField jFormattedTextField3;
private javax.swing.JFormattedTextField jFormattedTextField4;
private javax.swing.JFormattedTextField jFormattedTextField5;
private javax.swing.JFormattedTextField jFormattedTextField6;
private javax.swing.JFormattedTextField jFormattedTextField7;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
// End of variables declaration
}
Código TelaPrincipal.java:
package telas;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
/**
-
@author abmc0
*/
public class TelaPrincipal extends javax.swing.JFrame {
private final ArrayList listaCarros;public TelaPrincipal() {
initComponents();
listaCarros = new ArrayList<>();
}public void adcionarCarro(Carro carro){
listaCarros.add(carro);
atualizarTabela();
}public void adicionarLinhaNaTabela(String marca, String modelo, String cor, String ano, String quilometragem, String preco) {
DefaultTableModel model = (DefaultTableModel) minhatabela.getModel();
model.addRow(new Object[]{marca, modelo, cor, ano, quilometragem, preco});
}public void adicionarLinhaTabela(String marca, String ano, String modelo, String cor, String km, String preco) {
DefaultTableModel model = (DefaultTableModel) minhatabela.getModel();
model.addRow(new Object[]{marca, ano, modelo, cor, km, preco});
}/**
-
This method is called from within the constructor to initialize the form.
-
regenerated by the Form Editor.
*/
@SuppressWarnings(“unchecked”)
//
private void initComponents() {jLabel2 = new javax.swing.JLabel();
desktop = new javax.swing.JDesktopPane();
jScrollPane1 = new javax.swing.JScrollPane();
minhatabela = new javax.swing.JTable();
lbldata = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
menu = new javax.swing.JMenuBar();
menAjudaSobre = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
menOpSair = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“TOP CARS - Controle de Estoque”);
setBackground(new java.awt.Color(255, 255, 255));
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource(“/icons/topcarlogo-.png”))); // NOI18N
minhatabela.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {}, new String [] { }
));
minhatabela.setGridColor(new java.awt.Color(0, 0, 100));
minhatabela.setName(“tabela01”); // NOI18N
minhatabela.setShowGrid(true);
jScrollPane1.setViewportView(minhatabela);desktop.setLayer(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout desktopLayout = new javax.swing.GroupLayout(desktop);
desktop.setLayout(desktopLayout);
desktopLayout.setHorizontalGroup(
desktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, desktopLayout.createSequentialGroup()
.addContainerGap(72, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 733, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(67, 67, 67))
);
desktopLayout.setVerticalGroup(
desktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
);lbldata.setFont(new java.awt.Font(“Segoe UI”, 1, 18)); // NOI18N
lbldata.setText(“25/10”);jButton1.setBackground(new java.awt.Color(0, 204, 0));
jButton1.setForeground(new java.awt.Color(0, 0, 0));
jButton1.setText(“ADICONAR”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});jButton2.setBackground(new java.awt.Color(255, 0, 51));
jButton2.setForeground(new java.awt.Color(0, 0, 0));
jButton2.setText(“REMOVER”);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});jLabel1.setFont(new java.awt.Font(“Segoe UI”, 1, 14)); // NOI18N
jLabel1.setText(“Adicionar ao estoque”);jLabel3.setFont(new java.awt.Font(“Segoe UI”, 1, 14)); // NOI18N
jLabel3.setText(“Remover do estoque”);menAjudaSobre.setText(“Ajuda”);
jMenuItem2.setText(“Sobre”);
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
menAjudaSobre.add(jMenuItem2);menu.add(menAjudaSobre);
menOpSair.setText(“Opções”);
menOpSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menOpSairActionPerformed(evt);
}
});jMenuItem3.setText(“Sair”);
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
menOpSair.add(jMenuItem3);menu.add(menOpSair);
setJMenuBar(menu);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(desktop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(lbldata)
.addGap(150, 150, 150))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(desktop)
.addGroup(layout.createSequentialGroup()
.addComponent(lbldata)
.addGap(96, 96, 96)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
);setSize(new java.awt.Dimension(1236, 609));
setLocationRelativeTo(null);
}//
private void formWindowActivated(java.awt.event.WindowEvent evt) {
Date data = new Date();
DateFormat formatador = DateFormat.getDateInstance(DateFormat.SHORT);
lbldata.setText(formatador.format(data));}
private void menOpSairActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
int sair = JOptionPane.showConfirmDialog(null, “Tem Certeza que deseja sair?”,“Atenção”,JOptionPane.YES_NO_OPTION);
if (sair == JOptionPane.YES_OPTION){
System.exit(0);
}
}private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
TelaSobre sobre = new TelaSobre();
sobre.setVisible(true);
}private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void abrirTelaAdcionar() {
TelaAdcionar telaAdcionar = new TelaAdcionar(this);
telaAdcionar.setVisible(true);
}
private void atualizarTabela() {
// Obtém o modelo da tabela
DefaultTableModel modelo = (DefaultTableModel) minhatabela.getModel();// Limpa todas as linhas atuais da tabela
modelo.setRowCount(0);// Adiciona cada carro do ArrayList como uma nova linha na tabela
for (Carro carro : listaCarros) {
Object[] linha = {
carro.getMarca(),
carro.getAno(),
carro.getModelo(),
carro.getQuilometragem(),
carro.getCor(),
carro.getPreco()
};
modelo.addRow(linha);
}TelaPrincipal telaPrincipal = new TelaPrincipal();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new TelaAdcionar(this).setVisible(true);
DefaultTableModel model = (DefaultTableModel)minhatabela.getModel();
model.addRow(new Object[]{“Marca”, “Ano”, “Modelo”, “Cor”, “km”, “Preço”});
}public static void main(String args[]) {
/* Set the Nimbus look and feel /
//
/ If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With Swing > Modifying the Look and Feel)
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Nimbus”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
///* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TelaPrincipal().setVisible(true); } });
}
// Variables declaration - do not modify
private javax.swing.JDesktopPane desktop;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lbldata;
private javax.swing.JMenu menAjudaSobre;
private javax.swing.JMenu menOpSair;
private javax.swing.JMenuBar menu;
private javax.swing.JTable minhatabela;
// End of variables declaration
} -