How can I display a table recovered from MySQL (in an ArrayList) and display it on Java Swing using JTable.I think that that recovery of the table from MySQL is done. My problem is in displaying it on JTable.
Any help will be appreciated.
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);
}
}
For the part where you want to show the records from the TBiens
table in the Swing JTable
, you can take a look at the following Stack Overflow question: Load arrayList data into JTable.
It is not clear to me what type of objects are in the biens
list and which columns the TBiens
table has in the database.
Note: I think you will also want to make the biens
list accessible for the code below, since it is now only visible in the action listener of the btnAfficher
button. When you have solved this, you can call a fillTable
method to fill the Swing JTable
with the right data:
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);
This fillTable
method could look like this:
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());
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다