ScrollPaneLayout – Java Swing – Example
In this tutorial, we are going to see an example of ScrollPaneLayout in Java Swing. JScrollPaneLayout is responsible for nine components: a window, two scrollbars, a row header, a column header, and four corners.
Constructor of ScrollPaneLayout class:
- ScrollPaneLayout(): it is used to build a new ScrollPanelayout.
Commonly used methods of ScrollPaneLayout are:
- removeLayoutComponent(Component comp): removes the specified component.
- getColumnHeader(): it returns the object JViewport which is the column header.
- getVerticalScrollBar(): returns the object JScrollBar that handles vertical scrolling.
- getHorizontalScrollBar(): returns the object JScrollBar that handles horizontal scrolling.
- addLayoutComponent(String st, Component c): adds the specified component.
- getViewport(): returns the object JViewport that displays the scrolling content.
- getCorner(String key): it is used to return the component to the specified corner.
Example: ScrollPaneLayout in Java Swing
import javax.swing.*;
public class MyScrollPane extends JFrame
{
public MyScrollPane()
{
ImageIcon img = new ImageIcon("test.png");
JScrollPane png = new JScrollPane(new JLabel(img));
getContentPane().add(png);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new MyScrollPane();
}
}
Output:





