简体   繁体   English

如何从另一个类访问 DefaultTableModel

[英]How to access DefaultTableModel from another class

I intend to have a form (FLlistes) that shows a table populated with data from taken from a database with hibernate.我打算有一个表格(FLlistes),它显示一个表格,其中填充了从休眠数据库中获取的数据。 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:我在 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
}

I have another class that makes the queries to populate the table with Hibernate:我有另一个类,它使查询以使用 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);问题是我不知道如何在model.addRow(row);访问模型model.addRow(row); in order to fill the table为了填表

Make FLlistes as singletone and provide a getter method for DefaultTableModel property.FLlistes设为FLlistes并为DefaultTableModel属性提供 getter 方法。 Then you can access getModel() from singletone object .然后你可以从单调对象访问getModel()

public class FLlistes extends JInternalFrame {

private JTable table;
private DefaultTableModel model;

public DefaultTableModel  getModel(){
  return model;
}

//Singletone implemenation 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM