1

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

wmash
  • 4,032
  • 3
  • 31
  • 69
  • It's probably something to do with the layouts. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 15 '15 at 13:14
  • `newSoftwareBtn.setBounds(..` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 15 '15 at 13:15

2 Answers2

2

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.

Pandatyr
  • 284
  • 2
  • 8
  • Thank you for your help! I have added the ScrollPane to the Panel but it is very small. How can I set the content pone of the panel so it is the same size? Something like `panel.getContentPane().add(scollPane)` – wmash Sep 15 '15 at 13:49
  • You can adjust the size of the scrollPane by using [awt.Component.setPreferredSize](https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#setPreferredSize%28java.awt.Dimension%29). As an argument, you need to pass [awt.Dimension](https://docs.oracle.com/javase/7/docs/api/java/awt/Dimension.html). For your purpose, you'll want to use the third constructor to set width and height to whatever you'd like. This might look like this: `scrollPane.setPreferredSize(new Dimension(300,300))` – Pandatyr Sep 15 '15 at 13:56
  • Thank you! Now for some reason, my table has does not display in my ScrollPane, even though I have...`scollPane.add(cSoftware)` – wmash Sep 15 '15 at 14:05
  • Try using [this constructor](https://docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html#JScrollPane%28java.awt.Component%29) for your JScrollPane, passing the table as argument. – Pandatyr Sep 15 '15 at 14:17
  • I have fixed it by adding `scrollPane.getViewport().add(cSoftware)`. Thank you for your help! – wmash Sep 15 '15 at 14:19
1

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()
Eric Leibenguth
  • 4,167
  • 3
  • 24
  • 51