2

I'm creating GUI for my university project and I 'm trying to understand how JScrollPane works.

I have successfully written simple program which shows picture in a scrollable way:

public class ScrollPaneTest{
    public static void main(String[] args){
        JFrame testFrame = new JFrame("ramka testowa");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel picture = new JLabel(new ImageIcon("JavaSwingCompoentsList.PNG"));
        JScrollPane scrollPane = new JScrollPane(picture);

        testFrame.add(scrollPane, BorderLayout.CENTER);
        testFrame.setSize(400, 400);
        testFrame.setVisible(true);
    }
}

Although, in my final GUI, I would like to apply JScrollPane only to a part of it, e.g. single JPanel. To test this idea I have written following code, which unfortunately does not work:

public class ScrollPaneTest{
    public static void main(String[] args){
        JFrame testFrame = new JFrame("ramka testowa");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel picture = new JLabel(new ImageIcon("JavaSwingCompoentsList.PNG"));
        JScrollPane scrollPane = new JScrollPane(picture);

        JPanel insidePanel = new JPanel();
        insidePanel.add(scrollPane);
        testFrame.add(insidePanel, BorderLayout.CENTER);

        testFrame.setSize(400, 400);
        testFrame.setVisible(true);

    }
}

I have read numerous tutorials, as well as Stack and CodeRanch articles and I still fail to grasp the idea of how the JScrollPane work. I suspect, that my mistake has something to do with specifying the dimension of JPanel-to-scroll, but every single approach I have tried gave me no scrollbars or no picture at all.

If you could show me the right solution to this problem and most importantly, show me where I was wrong I would be very grateful.

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
Karol Pokomeda
  • 123
  • 2
  • 13

3 Answers3

3
  1. Initialize JPanel, not JScrollPane with picture
  2. To add the JPanel to JScrollPane, do:

    scrollPane.setViewportView (panel)
    
  3. Add the JScrollPane, not JPanel to the JFrame

Frakcool
  • 10,540
  • 9
  • 46
  • 78
Top Sekret
  • 698
  • 5
  • 21
2

The simplest way is to create your JPanel and specify it when creating the JScrollPane:

 JPanel myPanel = ...;
 JScrollPane scroller = new JScrollPane( myPanel );

Then just add the scroller to your GUI (instead of adding myPanel ).

FredK
  • 4,046
  • 1
  • 8
  • 11
2

The problem seems to come from the default FlowLayout of the inner panel. Change it to another layout (I use BordeLayout) and it should work. Having said that, I cannot explain why flow layout fails!

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class ScrollPaneTest {

    public static void main(String[] args) {
        JFrame testFrame = new JFrame("ramka testowa");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel picture = new JLabel(new ImageIcon(
                new BufferedImage(370, 1200, BufferedImage.TYPE_INT_RGB)));
        JScrollPane scrollPane = new JScrollPane(picture);

        JPanel insidePanel = new JPanel(new BorderLayout());
        insidePanel.add(scrollPane);
        testFrame.add(insidePanel, BorderLayout.CENTER);

        insidePanel.add(new JLabel("Stay"), BorderLayout.LINE_START);
        insidePanel.add(new JLabel("Stay"), BorderLayout.LINE_END);
        insidePanel.add(new JLabel("Stay"), BorderLayout.PAGE_START);
        insidePanel.add(new JLabel("Stay"), BorderLayout.PAGE_END);

        testFrame.pack();

        testFrame.setSize(400, 400);
        // failing to do this will end the main & the app.
        // doing it will cause the EDT to start.
        testFrame.setVisible(true);
    }
}
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420