如何从另一个类访问DefaultTableModel [英] How to access DefaultTableModel from another class

查看:72
本文介绍了如何从另一个类访问DefaultTableModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算有一个表格(FLlistes),该表格显示一个表,该表中填充了来自休眠状态的数据库中的数据.问题是我不知道如何从用于创建查询的类中访问表(或表模型).

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.

我在JInternal框架中创建了一个jtable,如下所示:

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
}

我还有另一个类,使查询使用Hibernate填充表:

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

问题是我不知道如何在 model.addRow(row); 中访问模型以填充表格

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

推荐答案

FLlistes 设为单调,并为 DefaultTableModel 属性提供一种吸气方法.然后,您可以从单调对象访问 getModel().

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 
}

这篇关于如何从另一个类访问DefaultTableModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆