I have loaded the rows of the data from the database in the list.Now,i am trying to display the list of datas on the JPanel which is loaded inside the parent JPanel on button click. I have retreived the list of data but not able to display the data on the JPanel.
Any guides will be appreciated.

public class example
{
    private JPanel mainPanel = new JPanel();
 
    public example()
    {
        String[] header = 
        {
            "Header 1", "Header 2" ///////add colun name
        };
        String[][] data = new String[20][2];//put data received from database...
        for (int i = 0; i < data.length; i++)
        {
            for (int j = 0; j < data[i].length; j++)
            {
                data[i][j] = "Data [" + i + "][" + j + "]"; 
            }
        }
        JTable table = new JTable(data, header);
        
        JPanel sidePanel = new JPanel();
        JLabel sideLabel = new JLabel("Side Label");
        sidePanel.add(sideLabel);
        
        JPanel viewPanel = new JPanel();
        viewPanel.setLayout(new BorderLayout());
        viewPanel.add(table, BorderLayout.CENTER);
        viewPanel.add(sidePanel, BorderLayout.EAST);
        
        
        JScrollPane scroll = new JScrollPane(viewPanel);
        scroll.setColumnHeaderView(table.getTableHeader());
        
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(scroll, BorderLayout.CENTER);
        
    }
 
    public JPanel getMainPanel()
    {
        return mainPanel;
    }
 
    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("TableAndStuff");
        frame.getContentPane().add(new example().getMainPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
 
    public static void main(String[] args)
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.