1

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!");
    }
}
Deniss M.
  • 3,617
  • 17
  • 52
  • 100
  • 1
    You would add the TableModelListener to your TableModel (seems obvious, no?), TripAbstractTableModel. Consider creating a TripAbstractTableModel variable, using it to create your JTable, and adding your model listener to it. The `fireTableChanged()` method call goes inside the model itself, not in the listener or any code outside of your TableModel. It is called when the data in the model changes significantly, and especially where the more specific fireXxx methods don't apply. – Hovercraft Full Of Eels Jun 27 '15 at 15:51
  • Thanks!!! hmmm. I think I've tried that and it didn't work. Can you please show with a code how you would do it? – Deniss M. Jun 27 '15 at 18:19
  • 1
    .... um you first perhaps. If you've tried it, why not show us this attempt? – Hovercraft Full Of Eels Jun 27 '15 at 18:42
  • Thanks again! I've tried it like this, but I can't figure out where to put the **fireTableChanged()**. I've tried some logical places, like inside the button **addActionListener()** in ViewDataArea.java, gives errors. I think my head just stopped reacting to this issue. Here's the adapted code in **TripAbstractTableModel.java**: `public TripAbstractTableModel() { super(); TripAbstractTableModel model = new TripAbstractTableModel(); model.addTableModelListener(this); SessionFactory sf = new Configuration().configure().buildSessionFactory();` – Deniss M. Jun 27 '15 at 19:01
  • Sorry about that. Adapted **TripAbstractTableModel.java** above in the question. Thanks! – Deniss M. Jun 27 '15 at 19:09

0 Answers0