Search code examples
javaswingjtabletablemodel

Why is my JScrollpane not updating remove changes despite having validate() and repaint()


I have a terrible problem right now that's dooming my program at the moment. It seems that I can add elements to my ScrollPane via Jtable that has a custom model. It's fine when I add elements, the table updates and rows appear, etc etc. The real problem is when I remove elements it does not update the table no matter what. As a matter of fact, if I minimize the applet, then bring it up again, all the elements are now gone, but! the problem is that if I try to add new elements, they don't appear on ScrollPane even though the arraysize does grow when I add and shows my new elements when I println them. I've tried many things (you'll see with my repaints etc) but not sure what else i can do. thanks for any help :)

    modelx = new HumanListModel();
    final JTable newbiex = new JTable(modelx);  

    newbiex.setDefaultRenderer(Human.class, new HumanRenderer());
    newbiex.setFillsViewportHeight(true);
    newbiex.setVisible(true);
    playerViewer.setViewportView(newbiex);


    removeIndividual.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent event)
        {

            playerViewer.remove(newbiex.getSelectedRow());
            playerViewerExhibition.remove(newbiex.getSelectedRow());
            ///modelx.remove(newbiex.getSelectedRow());///

            modelx.removeElementAt(newbiex.getSelectedRow());

            indexPoint -= 1;///helps keep track of index///

            newbiex.repaint();
            newbiex.revalidate();
            newbiey.repaint(); ///table on another tab using modelx///
            newbiey.revalidate();///table on another tab///
            playerViewer.repaint();
            playerViewer.revalidate();
            System.out.println(modelx.size());

        }
    });

Solution

  • modelx.removeElementAt(newbiex.getSelectedRow());
    

    The TableModel doesn't have a method by this name so you must be creating a custom method. Make sure this method invokes fireTableRowsDeleted(...). You can always look at the source code of the DefaultTableModel to see how this is done.

    newbiex.repaint();
    newbiex.revalidate();
    newbiey.repaint(); ///table on another tab using modelx///
    newbiey.revalidate();///table on another tab///
    playerViewer.repaint();
    playerViewer.revalidate();
    

    None of the above code is needed. If you fire the appropriate events from the model the table will automatically repaint itself.

    if I minimize the applet, then bring it up again, all the elements are now gone, but! the problem is that if I try to add new elements, they don't appear on ScrollPane even though the arraysize does grow when I add and shows my new elements when I println them. I've tried many things (you'll see with my repaints etc) but not sure what else i can do. thanks for any help :)

    I would say you have implemented your TableModel incorrectly. Try using the DefaultTableModel. It supports dynamic addition/removal of rows.

    Or you can check out Row Table Model. You would only need to implement a couple of methods. The JButtonTableModel gives a simple example of how to do this.