-1

I am using a JTable. I want to get a notification whenever a cell selection value changes. But my code isn't working. I'm using TableModelListener. Nothing happens when I change value of table cell.

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);
    }
}

Nothing happened when I changed the value of a table cell.

nick zoum
  • 7,216
  • 7
  • 36
  • 80
Ken Li
  • 1
  • 1
  • http://stackoverflow.com/questions/12376826/jtable-cell-listener – alex Oct 21 '16 at 11:05
  • 1
    Works fine for me: http://stackoverflow.com/questions/3540661/tablemodellistener-and-multiple-column-validation/3541876#3541876. If you need more help then post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. We are not here to read the code of your entire application. So your SSCCE would just be a JFrame with a JTable, then you will easily be able to see your problem. Once you get this working you then see how your application is different. – camickr Oct 21 '16 at 14:54

1 Answers1

3

You are setting the TableModelListener then setting a new TableModel. Of course the old model is gone and the new model is not listened.

Try switching the order, like in:

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());

    }
});
Adrian Colomitchi
  • 3,974
  • 1
  • 14
  • 23