美好的一天!
我一直试图用tableModel监听器来解决这个问题,但似乎无法理解它是如何工作的,以及在哪里添加 addTableModelListener 和 fireTableChanged 以便我的代码开始监听数据库中的表数据更改。真的很感激一些投入。提前致谢!这是代码:
ViewDataArea.java
import trip.TripAbstractTableModel;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ViewDataArea extends JComponent {
int yPosition = 60;
int xLabelPosition = 20;
int xFieldPosition = 220;
int labelWidth = 200;
int fieldWidth = 200;
int tableHeight = 400;
int tableWidth = 1155;
JButton emptyButton = new JButton("Refresh");
int buttonHeight = 28;
protected JPanel panel() {
JPanel panel = new JPanel();
panel.setLayout(null);
final JTable table = new JTable(new TripAbstractTableModel());
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(xLabelPosition, yPosition, tableWidth, tableHeight);
panel.add(scrollPane);
emptyButton.setBounds(xLabelPosition, yPosition - 30, labelWidth, buttonHeight);
panel.add(emptyButton);
emptyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
return panel;
}
}
TripAbstractTableModel.java
package trip;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
public class TripAbstractTableModel extends AbstractTableModel implements TableModelListener {
private static final long serialVersionUID = 6105842825518764825L;
private ArrayList<trip.TripEntity> tripList;
public TripAbstractTableModel() {
super();
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Query q = session.createQuery("from trip.TripEntity");
tripList = new ArrayList<TripEntity>(q.list());
session.close();
sf.close();
}
public int getRowCount() {
return tripList.size();
}
public int getColumnCount() {
return 5;
}
public Object getValueAt(int rowIndex, int columnIndex) {
trip.TripEntity trip = tripList.get(rowIndex);
Object [] values = new Object[]{
trip.getId(), trip.getTripNumber(), trip.getExportCountry(), trip.getDestinationCountry(),
trip.getPriceFixed()};
return values[columnIndex];
}
@Override
public String getColumnName(int column) {
String[] columnNames = new String[]{"id","tripnumber","exportcountry","destinationcountry","pricefixed"};
return columnNames[column];
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void tableChanged(TableModelEvent e) {
System.out.println("Table Alert!");
}
}
更新:编辑过TripAbstractTableModel.java
package trip;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
public class TripAbstractTableModel extends AbstractTableModel implements TableModelListener {
private static final long serialVersionUID = 6105842825518764825L;
private ArrayList<trip.TripEntity> tripList;
public TripAbstractTableModel() {
super();
TripAbstractTableModel model = new TripAbstractTableModel();
model.addTableModelListener(this);
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Query q = session.createQuery("from trip.TripEntity");
tripList = new ArrayList<TripEntity>(q.list());
session.close();
sf.close();
}
public int getRowCount() {
return tripList.size();
}
public int getColumnCount() {
return 5;
}
public Object getValueAt(int rowIndex, int columnIndex) {
trip.TripEntity trip = tripList.get(rowIndex);
Object [] values = new Object[]{
trip.getId(), trip.getTripNumber(), trip.getExportCountry(), trip.getDestinationCountry(),
trip.getPriceFixed()};
return values[columnIndex];
}
@Override
public String getColumnName(int column) {
String[] columnNames = new String[]{"id","tripnumber","exportcountry","destinationcountry","pricefixed"};
return columnNames[column];
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void tableChanged(TableModelEvent e) {
System.out.println("Table Alert!");
}
}