如何显示从MySQL恢复的表(在ArrayList中)并使用JTable在Java Swing上显示它。我认为从MySQL恢复表已完成。我的问题是在JTable上显示它。
任何帮助将不胜感激。
Class Afficher.java
package com.hibernate.stock;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.Query;
import javax.swing.JTable;
import javax.swing.JTextArea;
public class Afficher extends JFrame {
private JPanel contentPane;
private JTable table;
ArrayList<Object[]> biens= new ArrayList<Object[]>();
/**
* Launch the application.
*/
private void fillTable(final JTable table, final List biens) {
final String columnNames[] = {"ID", "Nom", "Catégorie", "Quantité"};
final DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
table.setModel(tableModel);
for (final Object bien : biens) {
// Assuming each row in the biens list is a list of strings...
final List<Object> row = (List<Object>) bien;
tableModel.addRow(row.toArray());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Afficher frame = new Afficher();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Afficher() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnAfficher = new JButton("Afficher");
btnAfficher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
SQLQuery query=s.createSQLQuery("select * from TBiens");
biens = (ArrayList) query.list();
fillTable(table, biens);
s.flush();
tx.commit();
s.close();
}
});
btnAfficher.setBounds(166, 235, 117, 25);
contentPane.add(btnAfficher);
JTable table = new JTable();
fillTable(table, biens);
// I put the table in a scroll pane and had to make it bigger...
final JScrollPane tableScrollPane = new JScrollPane(table);
tableScrollPane.setBounds(224, 90, 400, 500);
contentPane.add(tableScrollPane);
}
}
答案 0 :(得分:0)
对于要在Swing TBiens
中显示JTable
表中记录的部分,您可以查看以下Stack Overflow问题:Load arrayList data into JTable。
我不清楚biens
列表中的对象类型以及TBiens
表在数据库中的哪些列。
注意:我认为您还希望以下代码可以访问biens
列表,因为它现在只能在btnAfficher
按钮的动作侦听器中显示。解决了这个问题后,您可以调用fillTable
方法用正确的数据填充Swing JTable
:
JTable table = new JTable();
fillTable(table, biens);
// I put the table in a scroll pane and had to make it bigger...
final JScrollPane tableScrollPane = new JScrollPane(table);
tableScrollPane.setBounds(224, 90, 400, 500);
contentPane.add(tableScrollPane);
此fillTable
方法可能如下所示:
private void fillTable(final JTable table, final List biens) {
final String columnNames[] = {"Column A", "Column B", "Column C"};
final DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
table.setModel(tableModel);
for (final Object bien : biens) {
// Assuming each row in the biens list is a list of strings...
final List<String> row = (List<String>) bien;
tableModel.addRow(row.toArray());
}
}
答案 1 :(得分:0)
它终于工作了@Freek de Bruijn :),我编辑了ActionPerformed方法:
`public void actionPerformed(ActionEvent arg0) {
try{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
SQLQuery query=s.createSQLQuery("select * from TBiens");
biens = query.list();
ArrayList<Object[]> res = new ArrayList<Object[]>(biens);
final String columnNames[] = {"ID", "Nom", "Catégorie", "Quantité"};
final DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
table.setModel(tableModel);
for (final Object[] bien : res) {
// Assuming each row in the biens list is a list of strings...
final Object[] row = bien;
tableModel.addRow(row);
}
biens.size();
System.out.print(i);
s.flush();
tx.commit();
s.close();
}
catch (ClassCastException e) {
e.printStackTrace();
}
}
});`
非常感谢你。