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.