Search code examples
javaswingjtableabstracttablemodel

JTable not showing


In my application everything is distributed.

  • On a action, application retrieves data from DB and saves in ArrayList<T>.
  • I create an object of RelativeTableModel where I pass the ArrayList<T>.

    public void RelationsClicked() {
        ArrayList<Relation> data = myParent.dbOperation.getRelations();
        RelativeTableModel tModel = new RelativeTableModel(data);  // subclass of AbstractTableModel
        myParent.SetBrowsePanelData(tModel);
        myParent.SetMainPanel(CashAccountingView.BROWSEPANEL);
    }
    
    • I have a BrowseListPanel class that has a JTable in JScrollPane. Its instance is already created in the main application.
    • I pass the model to BrowseListPanel and finally show the panel.

Code:

public void SetBrowsePanelData(AbstractTableModel tModel) {
    browsePanel.setTModel(tModel);
}

// BrowseListPanel's Code
public void setTModel(AbstractTableModel tModel) {
    this.tModel = tModel;  // tModel = AbstractTableModel
}

// Show the Panel
public void SetMainPanel(String panel) {
    activePanel = panel;
    SetFontSize();
    cards.show(mainPanel, panel);
    mainPanel.revalidate();
    mainPanel.repaint();
}

But I don't see the Table. I believe as the object of BrowseListPanel (containing the JTable) is already created & later the TableModel is added. So some sort of event should be fired in setTModel().

Am I right? If so, what event should be thrown and what should be its implementation.


Solution

  • Invoking setModel() on the table should be sufficient, but you might call fireTableStructureChanged() on the model explicitly as a way to help sort things out.

    Also, verify that you are working on the event dispatch thread.

    Addendum: Here's an sscce that shows the basic approach.

    enter image description here

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    /** @see http://stackoverflow.com/questions/8257148 */
    public class SwapTableModel extends JPanel {
    
        public SwapTableModel() {
            final JTable table = new JTable(Model.Alpha.model);
            table.setPreferredScrollableViewportSize(new Dimension(128, 32));
            this.add(new JScrollPane(table));
            final JComboBox combo = new JComboBox();
            for (Model model : Model.values()) {
                combo.addItem(model);
            }
            this.add(combo);
            combo.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    Model model = (Model) combo.getSelectedItem();
                    table.setModel(model.model);
                }
            });
        }
    
        private enum Model {
    
            Alpha(), Beta();
            private DefaultTableModel model;
    
            private Model() {
                Object[] data = {this.toString()};
                this.model = new DefaultTableModel(data, 1);
                model.addRow(data);
            }
        }
    
        private void display() {
            JFrame f = new JFrame("SwapTableModel");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new SwapTableModel().display();
                }
            });
        }
    }