Good day!
I've been trying to solve this riddle with the tableModel listener, but can't seem to understand how it works and where to add addTableModelListener and fireTableChanged so that my code starts to listen to table data changes in the database. Would really appreciate some input. Thanks in advance! Here is the code:
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!");
}
}
updated: edited 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!");
}
}