使用fireTableRowsInserted(int,int)更新Model和JTable

时间:2015-06-23 13:17:28

标签: java swing jtable pojo abstracttablemodel

我将行插入抽象表模型,然后尝试使用fireTableRowsInserted(int,int)更新JTable。它不起作用,我也尝试过其他“火”方法。我可以进行JTable更新的唯一方法是在其上调用repaint()。 fireTableRowsInserted(int,int)方法我缺少什么?

以下是示例代码。

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 个答案:

答案 0 :(得分:2)

您有addTableModelListener的空实现。您可以触发任何您想要的事件,如果没有人可以向您的模型注册监听器,那么听众将永远不会收到这些事件。

请从代码中删除以下部分

@Override
public void addTableModelListener(TableModelListener ml) {
}

@Override
public void removeTableModelListener(TableModelListener arg0) {
}