Search code examples
javaswingjtablejscrollpanenull-layout-manager

JTable not able to add scroll bars


I am having JTable which has records which can't fit into it so I have decided to add scrollbars. However, the scroll bar isn't appearing. What am I doing wrong?

new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

            table.setBounds(10, 73, 850, 850);

According to answers I have it like this now:

JScrollPane panel = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

            panel.setBounds(10, 73, 850, 850);
            contentPane.add(table);

Now the table isn't even shown.


Solution

  • contentPane.add(table);

    You are removing the table from the scrollpane.

    You must add the scroll pane to the frame. The code should be:

    contentPane.add(panel);