Olá, estava acompanhado uma vídeo aula por conta de um projeto e tá incompleta, preciso terminar o trabalho de escola e me ajudem a terminar o CRUD, por gentileza, o botão editar e excluir não funciona.
package View;
import Controller.AlunoControl;
import TableModel.AlunoTM;
import javax.swing.JOptionPane;
public class AlunoView extends javax.swing.JFrame {
AlunoTM Modelo;
public final void LoadTable() {
Modelo = new AlunoTM();
tbl_alunos.setModel(Modelo);
tbl_alunos.getColumnModel().getColumn(0).setPreferredWidth(100);
tbl_alunos.getColumnModel().getColumn(1).setPreferredWidth(250);
}
public void Botoes(boolean N, boolean E, boolean R, boolean S, boolean C) {
btn_novo.setEnabled(N);
btn_editar.setEnabled(E);
btn_excluir.setEnabled(R);
btn_salvar.setEnabled(S);
btn_cancelar.setEnabled(C);
}
public AlunoView() {
initComponents();
setLocationRelativeTo(null);
LoadTable();
tbl_alunos.setModel(Modelo);
Botoes(true, false, false, false, false);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tbl_alunos = new javax.swing.JTable();
btn_excluir = new javax.swing.JButton();
btn_novo = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
c_nome = new javax.swing.JTextField();
c_mat = new javax.swing.JTextField();
btn_cancelar = new javax.swing.JButton();
btn_salvar = new javax.swing.JButton();
btn_editar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tbl_alunos.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Matrícula", "Nome"
}
) {
boolean[] canEdit = new boolean [] {
false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
tbl_alunos.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tbl_alunosMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tbl_alunos);
if (tbl_alunos.getColumnModel().getColumnCount() > 0) {
tbl_alunos.getColumnModel().getColumn(0).setResizable(false);
tbl_alunos.getColumnModel().getColumn(1).setResizable(false);
}
btn_excluir.setText("Excluir");
btn_excluir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_excluirActionPerformed(evt);
}
});
btn_novo.setText("Novo");
btn_novo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_novoActionPerformed(evt);
}
});
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Aluno"));
jLabel1.setText("Matrícula:");
jLabel2.setText("Nome:");
btn_cancelar.setText("Cancelar");
btn_cancelar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_cancelarActionPerformed(evt);
}
});
btn_salvar.setText("Salvar");
btn_salvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_salvarActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel2))
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(c_mat, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(c_nome, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(131, 131, 131)
.addComponent(btn_salvar)
.addGap(18, 18, 18)
.addComponent(btn_cancelar)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(c_mat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(c_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_cancelar)
.addComponent(btn_salvar))
.addContainerGap(18, Short.MAX_VALUE))
);
btn_editar.setText("Editar");
btn_editar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_editarActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(btn_novo)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn_editar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_excluir)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_excluir)
.addComponent(btn_novo)
.addComponent(btn_editar))
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void tbl_alunosMouseClicked(java.awt.event.MouseEvent evt) {
Botoes(true, false, false, false, false);
int index = tbl_alunos.getSelectedRow();
if (index >= 0 && index < Modelo.getRowCount()) {
String[] temp = Modelo.getAluno(index);
c_mat.setText(temp[0]);
c_nome.setText(temp[1]);
}
}
private void btn_novoActionPerformed(java.awt.event.ActionEvent evt) {
Botoes(false, false, false, true, true);
}
private void btn_editarActionPerformed(java.awt.event.ActionEvent evt) {
Botoes(false, false, false, true, true);
}
private void btn_excluirActionPerformed(java.awt.event.ActionEvent evt) {
Botoes(true, false, false, false, false);
}
private void btn_salvarActionPerformed(java.awt.event.ActionEvent evt) {
if (AlunoControl.salvarAluno(c_mat.getText(), c_nome.getText())) {
this.LoadTable();
JOptionPane.showMessageDialog(this, "Aluno salvo com sucesso");
Botoes(true, false, false, false, false);
}
}
private void btn_cancelarActionPerformed(java.awt.event.ActionEvent evt) {
Botoes(true, false, false, false, false);
}
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(AlunoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AlunoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AlunoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AlunoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AlunoView().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_cancelar;
private javax.swing.JButton btn_editar;
private javax.swing.JButton btn_excluir;
private javax.swing.JButton btn_novo;
private javax.swing.JButton btn_salvar;
private javax.swing.JTextField c_mat;
private javax.swing.JTextField c_nome;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tbl_alunos;
// End of variables declaration
}
package Model;
import Util.Arquivo;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
/**
*
* @author BRoleg
*/
public class Aluno {
private String Matricula;
private String Nome;
public Aluno() {
}
public Aluno(String Matricula, String Nome) {
this.Matricula = Matricula;
this.Nome = Nome;
}
public Aluno(JSONObject json) {
this.Matricula = json.getString("matricula");
this.Nome = json.getString("nome");
}
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 JSONObject toJosn() {
JSONObject json = new JSONObject();
json.put("Nome", this.Nome);
json.put("Matricula", this.Matricula);
return json;
}
public boolean Persistir() {
JSONObject json = this.toJosn();
String base = Arquivo.Read();
JSONArray jA = new JSONArray();
if (!base.isEmpty() && base.length() > 5) {
jA = new JSONArray(base);
}
jA.put(json);
Arquivo.Write(jA.toString());
return true;
}
public static ArrayList<Aluno> getAlunos() {
ArrayList<Aluno> alunos = new ArrayList();
String base = Arquivo.Read();
if (base.isEmpty() || base.length() < 5) {
return null;
}
JSONArray jA = new JSONArray(base);
for (int i = 0; i < jA.length(); i++) {
Aluno A = new Aluno(jA.getJSONObject(i));
alunos.add(A);
}
return alunos;
}
}
package Controller;
import Model.Aluno;
import java.util.ArrayList;
/**
*
* @author BRoleg
*/
public class AlunoControl {
public static boolean salvarAluno(String Matricula, String Nome) {
Aluno a = new Aluno(Matricula, Nome);
return a.Persistir();
}
public static ArrayList<String[]> getAlunos() {
ArrayList<String[]> Alunos = new ArrayList();
ArrayList<Aluno> A = Aluno.getAlunos();
if (A != null) {
for (int i = 0; i < A.size(); i++) {
String a[] = new String[2];
a[0] = A.get(i).getMatricula();
a[1] = A.get(i).getNome();
Alunos.add(a);
}
}
return Alunos;
}
}
package TableModel;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
/**
*
* @author BRoleg
*/
public class AlunoTM extends AbstractTableModel {
private List<String[]> linhas;
private String[] colunas = new String[]{"Matricula", "Nome"};
public AlunoTM() {
linhas = new ArrayList<String[]>();
}
public AlunoTM(List<String[]> lista) {
linhas = new ArrayList<String[]>(lista);
}
@Override
public int getColumnCount() {
return colunas.length;
}
@Override
public int getRowCount() {
return linhas.size();
}
@Override
public String getColumnName(int columnIndex) {
return colunas[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
String t[] = linhas.get(rowIndex);
switch (columnIndex) {
case 0:
return t[0];
case 1:
return t[1];
default:
throw new IndexOutOfBoundsException("");
}
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
String t[] = linhas.get(rowIndex);
switch (columnIndex) {
case 0:
t[0] = aValue.toString();
case 1:
t[1] = aValue.toString();
}
fireTableCellUpdated(rowIndex, columnIndex);
}
public void setValueAt(String aValue[], int rowIndex) {
String t[] = linhas.get(rowIndex);
t[0] = aValue[0];
t[0] = aValue[1];
fireTableCellUpdated(rowIndex, 0);
fireTableCellUpdated(rowIndex, 1);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public String[] getAluno(int indiceLinha) {
return linhas.get(indiceLinha);
}
public void addAluno(String a[]) {
linhas.add(a);
int ultimoIndice = getRowCount() - 1;
fireTableRowsInserted(ultimoIndice, ultimoIndice);
}
public void remoove(int indiceLinha) {
linhas.remove(indiceLinha);
fireTableRowsDeleted(indiceLinha, indiceLinha);
}
public void addLista(List<String[]> a) {
int tamanhoAntigo = getRowCount();
linhas.addAll(a);
fireTableRowsInserted(tamanhoAntigo, getRowCount() - 1);
}
public void limpar() {
linhas.clear();
fireTableDataChanged();
}
public boolean isEmpty() {
return linhas.isEmpty();
}
}