Example of how to use JScrollPane
JScrollPane is a container that provides the ability to create horizontal or vertical scrolling or both at the same time when the displayed component exceeds the size of the JFrame.The code to create a scroll bar in java is very simple. The most popular use is JTextArea by adding a scroll pane to the .
import javax.swing.JFrame;Output
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class JScrollPaneTest {
//20-row, 50-column text box
JTextArea jta = new JTextArea(20, 50);
public JScrollPaneTest(){
JFrame f = new JFrame("JScrollPaneExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
JScrollPane jsp = new JScrollPane(jta);
f.add(jsp, BorderLayout.CENTER);
f.pack();
}
public static void main(String[] args) {
new JScrollPaneTest();
}
}
The scrollbar creates both horizontal and vertical bars if the text exceeds the dimension of the window.
JScrollBar and JViewPort
A JScrollPane combines the two classes JScrollBar et JViewPort et uses an instance of the latter to manage the part visible to the user. It is responsible for the dimension and positioning based on both vertical and horizontal elevators.
Here is the code to retrieve both scrollbars (horizontal and vertical):
Here is the code to retrieve both scrollbars (horizontal and vertical):
JScrollBar vjsp = jsp.getVerticalScrollBar();
JScrollBar hjsp = jsp.getHorizontalScrollBar();
The viewport is accessed with the method getViewport(). In this example, we'll add an image to JScrollPane and change the position of the viewport coordinates with the setViewPosition(Point p).
import java.awt.Point;Output
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;
public class JScrollPaneTest {
public JScrollPaneTest(){
JFrame f = new JFrame("JScrollPaneExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setSize(300,300);
ImageIcon image = new ImageIcon("Fleur.jpg");
JLabel pimage = new JLabel();
pimage.setIcon(image);
JScrollPane jsp = new JScrollPane(pimage);
JViewport jvp = new JViewport();
jvp = jsp.getViewport();
jvp.setViewPosition(new Point(30,200));
f.add(jsp);
}
public static void main(String[] args) {
new JScrollPaneTest();
}
}
Change settings
If the window is wide for example, the two bars disappear because we won't need it. If you decrease the height of the window, they will reappear. This is controlled by the scroll panel policy for each bar.
JScrollPane has two constructors that give you the hand to change the settings at creation time:
- JScrollPane(Component c, int vertical, int horizontal)
- JScrollPane(int vertical, int horizontal)
You can also change the display policy of the two bars after creation with the two methods: setHorizontalScrollBarPolicy(), setVerticalScrollBarPolicy().
VERTICAL_SCROLLBAR_AS_NEEDED HORIZONTAL_SCROLLBAR_AS_NEEDED | This is the case by default, scroll bars appear and disappear as needed. |
VERTICAL_SCROLLBAR_ALWAYS HORIZONTAL_SCROLLBAR_ALWAYS | Always show scroll bars. |
VERTICAL_SCROLLBAR_NEVER HORIZONTAL_SCROLLBAR_NEVER | scrollbars will always be hidden. |
The following statement is added to the code of the first JTextArea example right after JScrollPane:
References:jsp.setVerticalScrollBarPolicy(jsp. VERTICAL_SCROLLBAR_ALWAYS);Output:
Oracle documentation: JScrollPane
Oracle documentation: JViewPort
http://www2.htw-dresden.de/~beck/JAVA11/SWING/scrolling.html