Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Java
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
Java
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
Java
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.

What I have tried:

I search in google many times but I didn´t find an example using propertychangelistener in a entity class.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900