如何从另一个类访问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
}