简体   繁体   中英

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);

在此输入图像描述

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

在此输入图像描述

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.

I think you confuse a JScrollPane for a JScrollBar :

  • A JScrollPane is a wrapper to which you add large content, and which will show JScrollBar (either vertical or horizontal, or both) when this content exceeds the size of the container.
  • A JScrollBar is the actual horizontal or vertical scroll bar that you click and scroll.

In general you should work with a JScrollPane and let it take care of the scroll bars for you.

Based on the above, the answer to your question is:

  • Add the JTable to the JScrollPane ( new JScrollPane(table); ). You need only one JScrollPane in your situation.
  • Add the JScrollPane to the JPanel (To which you can also add your buttons)
  • Add the JPanel to the JFrame

Additionally:

  • You should use layout managers (By default, a JPanel has a FlowLayout , which might not be very convenient).
  • You don't need to add your table and your table's header separately.
  • You don't need to use repaint() or revalidate()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM