Search code examples
javaswingjtablejpaneljscrollpane

JScrollPane, add to JPanel or JTable?


I have Googled for hours on this issue but nothing seems to work.

I have a JTable with a JPanel inside a frame. The table has data from a database but there is a considerable amount of data to store (hence require the JScrollPane)

Here is my code:

public GeneralDisplay()
{
    Insets insets = getInsets();

    panel = new JPanel();
    scrollVert = new JScrollPane(panel);
    scrollVert.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    scrollHor = new JScrollPane(panel);
    scrollHor.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    newSoftwareBtn = new JButton("New");
    removeSofwtareBtn = new JButton("Remove");
    editSofwtareBtn = new JButton("Edit");

    ResultSet results;

    try
    {
        results = statement.executeQuery("SELECT * FROM Software");

        cSoftware = new JTable(buildTableModel(results));
    }
    catch(SQLException sqlEx)
    {
        JOptionPane.showMessageDialog(null, "Error: SQL error!");
        //System.exit(1);
    }

    ///////////////////////////////////////////////////
    //Adding to form
    ///////////////////////////////////////////////////

    getContentPane().add(panel);
    cSoftware.setBackground(null);
    cSoftware.getTableHeader().setBackground(null);
    //pack();

    panel.add(scrollVert);
    panel.add(scrollHor);
    panel.add(cSoftware.getTableHeader());
    panel.add(cSoftware);
    panel.add(newSoftwareBtn);
    panel.add(removeSofwtareBtn);
    panel.add(editSofwtareBtn);
    panel.add(scrollVert);
    panel.add(scrollHor);

    panel.revalidate();
    panel.repaint();

    //////////////////////////////////////////////////////

    //////////////////////////////////////////////////////
    //Position on form
    //////////////////////////////////////////////////////

    Dimension size = newSoftwareBtn.getPreferredSize();
    newSoftwareBtn.setBounds(5 + insets.left, 480 + insets.top, size.width, size.height);

    size = removeSofwtareBtn.getPreferredSize();
    removeSofwtareBtn.setBounds(55 + insets.left, 480 + insets.top, size.width, size.height);

    size = editSofwtareBtn.getPreferredSize();
    editSofwtareBtn.setBounds(105 + insets.left, 480 + insets.top, size.width, size.height);

enter image description here

In the image, the JScrollPane is visible in the area marked with a red square. I have more data in the table which is not visible, which is why I thought to add the JScrollPane to the table, but I also have buttons on my panel below the table which is why I wanted to add it to the panel.

My code might not be great as I have followed several tutorials on how to overcome the problem and kind of mashed them together.

Any help appreciated!

EDIT:

I have noticed that I added my scrolls to the panel twice. I have now removed that but still did not resolve the issue if that's what you thought it was

The other image is what happened when I added a GridLayout to the panel

enter image description here


Solution

  • Firstly, you don't need to create two JScrollPanes in order to have both a vertical and a horizontal scrollbar.

    As far as the ScrollPane being seperated from the rest of your components, you need to add your JTable to the ScrollPane and then add that to the JPanel. Something like this:

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    
    scrollPane.add(cSoftware);
    panel.add(scrollPane);
    //add buttons etc
    

    In your original code, you added both your (empty) ScrollPanels as well as your table to the JPanel.