How to access DefaultTableModel from another class

时光总嘲笑我的痴心妄想 提交于 2021-01-29 14:51:55

问题


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


回答1:


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 
}


来源:https://stackoverflow.com/questions/60564128/how-to-access-defaulttablemodel-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!