表模型侦听器无法正常工作

时间:2016-10-21 11:02:14

标签: java swing

我正在使用JTable。我希望每当单元格选择值发生变化时都会收到通知。但我的代码不起作用。我正在使用TableModelListener。当我更改表格单元格的值时没有任何反应。

public class Main extends JFrame{
    private JFrame frame;
    private DrawPanel jPanel;
    JRadioButton rdb2d;
    JRadioButton rdb3d;
    private JTable tbRect;
    private JTable tbCircle;

    List<Object> listCircle = new ArrayList<>();
    List<Object> listRect = new ArrayList<>();
    List<Object> undoCircle = new ArrayList<>();
    List<Object> undoRect = new ArrayList<>();
    List<Object> redoRedo = new ArrayList<>();

    public static void main(String[] args) {
        Main window = new Main();
        window.frame.setVisible(true);
    }


    public Main() { 
        frame = new JFrame();
        frame.setBounds(100, 100, 846, 494);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setVisible(true);

        jPanel = new DrawPanel();
        jPanel.setBounds(220, 76, 600, 369);
        frame.getContentPane().add(jPanel);

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(220, 11, 600, 54);
        frame.getContentPane().add(panel_1);

        JButton btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String[] buttons = { "Circle", "Rectangle" };
                int option = JOptionPane.showOptionDialog(null, "Choose a shape", "Confirmation", JOptionPane.WARNING_MESSAGE, 0, null, buttons, buttons[1]);
                if (rdb2d.isSelected()) {
                    AbstractFactory aFactory = new FactoryProducer().getFactory("2d");
                    Shape2D shape2d = null;
                    if (option == 0) {
                        shape2d = aFactory.getShape2D("circle");
                        listCircle.add(shape2d);
                        updateTableCircle(listCircle);
                    }else if (option == 1) {
                        shape2d = aFactory.getShape2D("rectangle");
                        listRect.add(shape2d);
                        updateTableRect(listRect);
                    }
                    jPanel.add(shape2d);
                }else if (rdb3d.isSelected()) {                 
                    AbstractFactory aFactory = new FactoryProducer().getFactory("3d");
                    Shape3D shape3d = null;
                    if (option == 0) {
                        shape3d = aFactory.getShape3D("circle");
                        listCircle.add(shape3d);
                        updateTableCircle(listCircle);
                    }else if (option == 1) {
                        shape3d = aFactory.getShape3D("rectangle");
                        listRect.add(shape3d);
                        updateTableRect(listRect);
                    }
                    jPanel.add(shape3d);
                }
            }
        });
        panel_1.add(btnAdd);

        JButton btnRemove = new JButton("Remove");
        btnRemove.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (tbCircle.getSelectedRow() != -1) {
                    jPanel.remove(listCircle.get(tbCircle.getSelectedRow()));
                    listCircle.remove(tbCircle.getSelectedRow());
                    updateTableCircle(listCircle);
                }
                if (tbRect.getSelectedRow() != -1) {
                    jPanel.remove(listRect.get(tbRect.getSelectedRow()));
                    listRect.remove(tbRect.getSelectedRow());
                    updateTableRect(listRect);
                }
            }
        });
        panel_1.add(btnRemove);

        JButton btnUndo = new JButton("Undo");
        btnUndo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                jPanel.undo();

            }
        });

        panel_1.add(btnUndo);

        rdb3d = new JRadioButton("3D");
        panel_1.add(rdb3d);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(rdb2d);
        buttonGroup.add(rdb3d);

        JPanel panel = new JPanel();
        panel.setBounds(10, 11, 200, 434);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        JScrollPane jPane = new JScrollPane();
        jPane.setBounds(0, 36, 200, 125);
        panel.add(jPane);

        tbCircle = new JTable();
        tbCircle.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                    "Position", "Radius"
                }
        ));

        jPane.setViewportView(tbCircle);
        tbCircle.getModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                System.out.println("Test Output");
            }
        });

        JScrollPane jPane2 = new JScrollPane();
        jPane2.setBounds(0, 248, 200, 163);
        panel.add(jPane2);

        tbRect = new JTable();

        tbRect.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                System.out.println(e.getColumn());

            }
        });

        tbRect.setModel(new DefaultTableModel(
            new Object[][]{},
            new String[]{
                "Position", "Width", "Height"
            }
        ));
        jPane2.setViewportView(tbRect);
    }
}

当我更改表格单元格的值时,没有任何反应。

1 个答案:

答案 0 :(得分:2)

您正在设置TableModelListener,然后设置新的TableModel。当然旧的模型已经消失,新的模型没有被听取。

尝试切换订单,例如:

tbRect = new JTable();

// set the model first
tbRect.setModel(new DefaultTableModel(
        new Object[][]{},
        new String[]{
            "Position", "Width", "Height"
        }
));

// register the model listener after the model is in place
tbRect.getModel().addTableModelListener(new TableModelListener() {
    @Override
    public void tableChanged(TableModelEvent e) {
        System.out.println(e.getColumn());

    }
});