Hi everyone, I hope you are well, I need to do the following query: It turns out that the Observer api is obsoleted since jdk 9, I was looking at propertychangelistener, but I have no idea how to apply it.
The situation is that I need that when a row is added, edited or deleted, the jtable is updated automatically, I put the codes:
Privilegio.java → entity
public class Privilegio implements Comparable<Privilegio>
{
private int id;
private String nombre;
private boolean activo;
private static int maxId = 0;
public Privilegio(int xid, String xnombre) throws Exception {
setId(xid);
setNombre(xnombre);
}
.....
}
PrivilegiosContr.java → Controller
public class PrivilegiosContr extends Adaptador<Privilegio> {
private static PrivilegiosContr gestoria;
private PagesMap<Integer, Privilegio> entidades;
public static PrivilegiosContr getInstancia(){
if(gestoria == null){
gestoria = new PrivilegiosContr();
}
return gestoria;
}
private PrivilegiosContr(){
entidades = new PagesMap();
}
@Override
public void borrar(Privilegio entidad) throws Exception {
if(verificarBorrado(entidad)){
entidad.setActivo(!entidad.isActivo());
} else {
entidades.remove(entidad.getId());
}
}
@Override
public void editar(Privilegio entidad) throws Exception {
Privilegio privilegio = obtenerPorId(entidad.getId());
if(!privilegio.equals(entidad)) {
if(entidades.containsValue(entidad)){
throw new Exception("El privilegio: " + entidad.getNombre() + " ya existe");
} else {
privilegio.setNombre(entidad.getNombre());
}
}
}
@Override
public void guardar(Privilegio entidad) throws Exception {
if(entidades.containsValue(entidad)){
throw new Exception("El privilegio: " + entidad.getNombre() + " ya existe");
} else {
entidad.setActivo(true);
entidades.put(entidad.getId(), entidad);
Privilegio.incrementarMaxId();
}
}
.....
}
BackendView.java → General View
public class BackendView extends javax.swing.JPanel {
TablaGenerica modP = new TablaGenerica(new ModeloTablaPrivilegio());
public BackendView(JFrame xventana) {
initComponents();
modP.cargarTablaGenerica(tabPrivilegios);
modP.getModelo().updateTable(null);
modP.getTabla().setComponentPopupMenu(ppmBackend);
modP.getTabla().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){
editarFila();
}
}
});
}
}
I’m just putting the necessary code, but the point is that I need to replicate the observer.
I wait your answers and greetings.