Java scrollpane not adding to panel instead displaying white screen

272 views Asked by At

So I have a table that is displaying data from a database but since there's a lot of data being outputted into the table it goes way beyond the bounds I have set for the JFrame.

I've tried adding a ScrollPane below but instead of allowing me to scroll down the GUI it will display white over the whole GUI! All I need to do is make it so I can scroll down to view the whole table!

Here is my code:

public class ViewAll extends JFrame{
    //Jtextfields, buttons, labels
    private static JButton one = new JButton("Back");
    private static JLabel lblMembTitle = new JLabel("<html><h1>All Members</h1></html>");
    private static JLabel lblPlayTitle = new JLabel("<html><h1>All Playlists</h1><br /></html>");
    //Containers, Panels, Scrollpanes
    private Container mainCon = this.getContentPane();
    private static JPanel pnlTable = new JPanel();


    //Tables
    private static JTable tblShowAllMemb = new JTable();
    private static JTable tblShowAllPlay = new JTable();
    JScrollPane scrollPane = new JScrollPane(pnlTable,   ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);



    public ViewAll(){
        super("Search/Edit/Delete Members");
        this.setBounds(400, 800, 854,400);
        //Add panel and create table model
        mainCon.add(pnlTable);
        MemTableModel tblMembers = new MemTableModel();
        PlayTableModel tblPlaylist = new PlayTableModel();
        this.add(scrollPane);

        //Add table and set tableModel
        pnlTable.add(lblMembTitle);
        pnlTable.add(tblShowAllMemb);
        tblShowAllMemb.setModel(tblMembers);

        pnlTable.add(lblPlayTitle);
        pnlTable.add(tblShowAllPlay);
        tblShowAllPlay.setModel(tblPlaylist);
    }

}
1

There are 1 answers

3
m.cekiera On BEST ANSWER

It seems that you add your JPanel pnlTable to multiple containers, first to scrollPane, then to Container mainCon (so it is not in scrollPane, as a component can be only in one container). So when you add scrollPane separetly to ViewAll it display nothing, because it is empty.

So use:

mainCon.add(scrollPane);

or delete mainCon.add(pnlTable); and use only:

this.add(scrollPane);