How to access DefaultTableModel from another class

winter :

I intend to have a form (FLlistes) that shows a table populated with data from taken from a database with hibernate. The problem is that I don't know how to access the table (or table model) from the class I use to create the queries.

I created a jtable in a JInternal frame like this:

public class FLlistes extends JInternalFrame {

    private JTable table;
    private DefaultTableModel model;

    //some code

    String[] columns = {"Id","Date", "Place", "Total"};
    model = new DefaultTableModel(columns, 0);
    table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(49, 176, 732, 361);
    getContentPane().add(scrollPane);
    scrollPane.setViewportView(model);

    //some code
}

I have another class that makes the queries to populate the table with Hibernate:

public class AccionsBD {

    public static void GetALLLlistes() {
        String jql = "select llc from LlistaCompra llc";

        EntityManager entityManager = JPAUtil.getEntityManagerFactory().createEntityManager();
        TypedQuery<LlistaCompra> q = entityManager.createQuery(jql,LlistaCompra.class);
        List<LlistaCompra> llistes = q.getResultList();

        for (LlistaCompra llista: llistes) {                
            String[] row = {Integer.toString(llista.getIdLlista()), llista.getData().toString(), llista.getLloc()};
            model.addRow(row);
        }
        entityManager.close();
    }
}

The problem is that i don't know how to access model in model.addRow(row); in order to fill the table

Dark Knight :

Make FLlistes as singletone and provide a getter method for DefaultTableModel property. Then you can access getModel() from singletone object .

public class FLlistes extends JInternalFrame {

private JTable table;
private DefaultTableModel model;

public DefaultTableModel  getModel(){
  return model;
}

//Singletone implemenation 
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27052&siteId=1