Updating Model and JTable with fireTableRowsInserted(int,int)

2.5k views Asked by At

I'm inserting rows to an Abstract Table Model, and then trying to update the JTable with fireTableRowsInserted(int,int). It doesn`t work, I have tried the other "fire" methods too. The only way I can have the JTable update is by calling repaint() on it. What am I missing with the fireTableRowsInserted(int,int) method?

Here is the sample code.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

public class Main {

    /**
     * A simple class
     * @author 
     *
     */
    public class CustObj {
        public double x;
        public double y;

        /**
         * Constructor
         * @param x
         * @param y
         */
        public CustObj(double x, double y) {
            this.x = x;
            this.y = y;
        }

        public void setX(Double x) {
            this.x = x;
        }

        public void setY(Double y) {
            this.y = y;
        }

        public Double getX() {
            return this.x;

        }

        public Double getY() {
            return this.y;

        }
    }

    @SuppressWarnings("serial")
    public class MotModel extends AbstractTableModel {

        private String[] columnNames = { "Col One", "Col Two" };
        private List<CustObj> custOL = new ArrayList<CustObj>(); //List Containing custom objects

        @Override
        public void addTableModelListener(TableModelListener ml) {

        }

        /**
         * Adds a row to the model
         * 
         * @param i
         * @param custObj
         */
        public void addRow(int i, CustObj custObj) {

            custOL.add(i, custObj);
            absModel.fireTableRowsInserted(0, custOL.size()); // does not work
            absModel.fireTableDataChanged(); // does not work
            // motTable.repaint(); //this works
        }

        @Override
        public Class<?> getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

        @Override
        public int getRowCount() {
            return custOL.size();
        }

        @Override
        public Object getValueAt(int row, int col) {
            CustObj ro = custOL.get(row);
            if (0 == col) {
                return ro.getX();
            } else if (1 == col) {
                return ro.getY();
            }
            return null;
        }

        @Override
        public boolean isCellEditable(int arg0, int arg1) {
            return true;
        }

        @Override
        public void removeTableModelListener(TableModelListener arg0) {
        }

        @Override
        public void setValueAt(Object da, int row, int col) {
            CustObj ro = custOL.get(row);
            if (col == 0) {
                ro.setX(Double.valueOf((String) da));
            } else if (col == 1) {
                ro.setY(Double.valueOf((String) da));
            }
            //this.fireTableCellUpdated(row, col);  //does not work
        }

        // Load some dummy objects
        public void init() {
            custOL.add(new CustObj(1.2, 2.1));
            custOL.add(new CustObj(1.4, 4.1));
        }
    }

    private JFrame frame;
    private MotModel absModel;  //Abstract Table Model
    private JTable table;     //JTable
    private JScrollPane scrollPane;
    private JButton btnDebug;

    private JButton btnAddRow;
    private JButton btnPaint;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main window = new Main();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application. Create the Abstract Table Model and JTable
     */
    public Main() {
        initialize();

        absModel = new MotModel();
        absModel.init();

        table = new JTable(absModel);
        table.setModel(absModel);
        table.setPreferredScrollableViewportSize(new Dimension(100, 70));
        table.setFillsViewportHeight(true);
        scrollPane.setViewportView(table);

        btnPaint = new JButton("Paint");
        btnPaint.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                table.repaint();
            }
        });
        btnPaint.setBounds(401, 268, 210, 23);
        frame.getContentPane().add(btnPaint);
        table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JTextField()));
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setResizable(false);
        frame.setBounds(100, 100, 805, 331);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        scrollPane = new JScrollPane();
        scrollPane.setBounds(173, 0, 438, 235);
        frame.getContentPane().add(scrollPane);

        btnDebug = new JButton("Debug");
        btnDebug.setBounds(173, 268, 210, 23);
        btnDebug.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                debug();
            }
        });

        btnAddRow = new JButton("Add Row");
        btnAddRow.setBounds(173, 240, 438, 23);
        btnAddRow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                absModel.addRow(0, new CustObj(1, 6)); // add an object
            }
        });
        frame.getContentPane().add(btnAddRow);
        frame.getContentPane().add(btnDebug);
    }

    /**
     * Print the values
     */
    protected void debug() {
        for (int i = 0; i < absModel.getRowCount(); i++) {
            CustObj obj = absModel.custOL.get(i);
            System.out.println(obj.x + " " + obj.y);
        }
    }
   }
1

There are 1 answers

0
Robin On BEST ANSWER

You have an empty implementation of addTableModelListener. You can fire whatever event you want, if nobody can register a listener to your model the events will never be received by the listeners.

So remove the following parts from your code

@Override
public void addTableModelListener(TableModelListener ml) {
}

@Override
public void removeTableModelListener(TableModelListener arg0) {
}