如何从另一个 class 访问 DefaultTableModel

How to access DefaultTableModel from another class

我打算有一个表格 (FLlistes),显示 table 填充了从休眠数据库中获取的数据。问题是我不知道如何从我用来创建查询的 class 访问 table(或 table 模型)。

我在这样的 JInternal 框架中创建了一个 jtable:

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
}

我有另一个 class 进行查询以使用 Hibernate 填充 table:

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();
    }
}

问题是我不知道如何访问 model.addRow(row); 中的模型来填充 table

FLlistes 设为单例并为 DefaultTableModel 属性 提供 getter 方法。然后你可以从 singleton object 访问 getModel()

public class FLlistes extends JInternalFrame {

private JTable table;
private DefaultTableModel model;

public DefaultTableModel  getModel(){
  return model;
}

//Singletone implemenation 
}