9
I have an example program that adds objects of my class type Pessoa
in a JTable
and also in a ArrayList<T>
, this program has three basic functionalities which are as follows::
- Add
- Alter
- Erase
See the program image:
In the event of clicking the button Add a type object is added Pessoa
in the JTable
and also in a ArrayList<T>
type of this class, see:
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
Pessoa pessoaAdd = new Pessoa(txtNome.getText(), obtemIdAtual(tablePessoas));
listaPessoas.add(pessoaAdd);
addPessoaToJTable(tablePessoas, pessoaAdd);
}
Notice that there is the method obtemIdAtual()
which is responsible for generating the id
sequentially, and as much as the values of the objects stored in the variable listaPessoas
and the values added to the JTable
tablePessoas
are identical.
Reproducing the example
To play this example you will need the code of the Person class and also the code corresponding to the program screen, are two files. Follow the complete codes below.
Class code Person:
package aquicolocaseupacote;
public class Pessoa {
private String nome;
public String getNome() { return nome; }
public void setNome(String nome) { this.nome = nome; }
private int id;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public Pessoa() { }
public Pessoa(String nome, int id) {
this.nome = nome;
this.id = id;
}
}
Screen code Mainexample:
package aquicolocaseupacote;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class MainExemplo extends JFrame {
private List<Pessoa> listaPessoas = new ArrayList<>();
public MainExemplo() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnApagar = new javax.swing.JButton();
btnAlterar = new javax.swing.JButton();
btnAdd = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tablePessoas = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
txtNome = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Exemplo");
setResizable(false);
btnApagar.setText("Apagar");
btnApagar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnApagarActionPerformed(evt);
}
});
btnAlterar.setText("Alterar");
btnAlterar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAlterarActionPerformed(evt);
}
});
btnAdd.setText("Add");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
tablePessoas.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Id", "Nome"
}
) {
boolean[] canEdit = new boolean [] {
false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(tablePessoas);
jLabel1.setText("Nome:");
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnAdd)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnAlterar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnApagar)
.addGap(0, 46, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtNome)))))
.addContainerGap())
);
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)
.addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnApagar)
.addComponent(btnAlterar)
.addComponent(btnAdd))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
Pessoa pessoaAdd = new Pessoa(txtNome.getText(), obtemIdAtual(tablePessoas));
listaPessoas.add(pessoaAdd);
addPessoaToJTable(tablePessoas, pessoaAdd);
}
private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {
switch (btnAlterar.getText()) {
case "Alterar":
btnAdd.setEnabled(false);
btnAlterar.setText("Salvar");
break;
case "Salvar":
btnAdd.setEnabled(true);
btnAlterar.setText("Alterar");
break;
}
}
private void btnApagarActionPerformed(java.awt.event.ActionEvent evt) {
}
void addPessoaToJTable(JTable jTable, Pessoa pessoa) {
DefaultTableModel model = (DefaultTableModel) jTable.getModel();
model.addRow(new Object[] { pessoa.getId(), pessoa.getNome() });
}
int obtemIdAtual(JTable jTable) {
DefaultTableModel model = (DefaultTableModel) jTable.getModel();
if (model.getRowCount() > 0) {
return (Integer) model.getValueAt(model.getRowCount() - 1, 0) + 1;
}
else {
return 1;
}
}
public static void main(String args[]) {
EventQueue.invokeLater(() -> {
new MainExemplo().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnAlterar;
private javax.swing.JButton btnApagar;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tablePessoas;
private javax.swing.JTextField txtNome;
// End of variables declaration
}
My difficulty
I’m not getting to implement the button routine Alter nor the button Erase, that is, when the user is going to make a change by clicking the change button the value (Person type object) must be changed both in the object that is in the variable listaPessoas
as in Jtable tablePessoas
and the same goes for the delete button.
I would like to know how I could make this implementation according to what this described above?
This is the solution to your problems https://answall.com/questions/121513/como-popular-uma-jtable-com-tablemodel-pr%C3%B3prio
– user28595