The layout manager used by JScrollPane. JScrollPaneLayout is based on nine components: a viewport, two scrollbars, a row header, a column header, and four “corner” components.
Constructor of the class:
- ScrollPaneLayout(): It is used to Construct a new ScrollPaneLayout.
Commonly Used Methods:
- removeLayoutComponent(Component comp): Removes the specified component from the layout.
- getColumnHeader(): It returns the JViewport object that is the column header.
- getVerticalScrollBar(): Returns the JScrollBar object that handles the vertical scrolling.
- getHorizontalScrollBar(): Returns the JScrollBar object that handles the horizontal scrolling.
- addLayoutComponent(String st, Component c): Adds the specified component to the layout.
- getViewport(): Returns the JViewport object that displays the scrollable contents.
- getCorner(String key):It is used to returns the Component at the specified corner.
Below programs illustrate the use of ScrollPaneLayout class:
1. The following program illustrates the use of ScrollPaneLayout by arranging several JLabel components in a JFrame, whose instance class is “Geeks“. We create one JScrollPane component named “scrollpane” and one JList component named “list“. We set the size and visibility of the frame by using setSize() and setVisible() method. The layout is set by using setLayout() method.
Java
// Java Program to illustrate the // ScrollPaneLayout class import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; // create a class Geeks extending JFrame public class Geeks extends JFrame { // Declaration of objects of the // JScrollPane class. JScrollPane scrollpane; // Constructor of Geeks class public Geeks() { // used to call super class // variables and methods super ( "JScrollPane Demonstration" ); // Function to set size of JFrame. setSize( 300 , 200 ); // Function to set Default close // operation of JFrame. setDefaultCloseOperation(EXIT_ON_CLOSE); // to contain a string value String categories[] = { "Geeks" , "Language" , "Java" , "Sudo Placement" , "Python" , "CS Subject" , "Operating System" , "Data Structure" , "Algorithm" , "PHP language" , "JAVASCRIPT" , "C Sharp" }; // Creating Object of "JList" class JList list = new JList(categories); // Creating Object of // "scrollpane" class scrollpane = new JScrollPane(list); // to get content pane getContentPane().add(scrollpane, BorderLayout.CENTER); } // Main Method public static void main(String args[]) { // Creating Object of Geeks class. Geeks sl = new Geeks(); // Function to set visibility of JFrame. sl.setVisible( true ); } } |
Output:
2. The following program illustrates the use of ScrollPaneLayout by arranging several JLabel components in a JFrame, whose instance class is named as “ScrollPanel”. We create one JScrollPane component named “scrollpane”. Also, JRadioButtonand ButtonGroup are created. We set the size and visibility of the frame by using setSize() and setVisible() method. The layout is set by using setLayout() method.
Java
// Java Program to illustrate the // ScrollPaneLayout class import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; // create a class ScrollPanel // extending JFrame public class ScrollPanel extends JFrame { // Declaration of objects of the // JScrollPane class JScrollPane scrollpane; // Constructor of ScrollPanel class public ScrollPanel() { // used to call super class // variables and methods super ( "JScrollPane Demonstration" ); // Function to set size of JFrame. setSize( 300 , 200 ); // Function to set Default // close operation of JFrame. setDefaultCloseOperation(EXIT_ON_CLOSE); init(); // Function to set // visible of JFrame. setVisible( true ); } // class init public void init() { // Declaration of objects // of JRadioButton class. JRadioButton form[][] = new JRadioButton[ 12 ][ 5 ]; // to contain a string count String counts[] = { "" , "1 star" , "2 star" , "3 star" , "4 star" , "5 star" }; // to contain a string value String categories[] = { "Geeks" , "Language" , "Java" , "Sudo Placement" , "Python" , "CS Subject" , "Operating System" , "Data Structure" , "Algorithm" , "PHP language" , "JAVASCRIPT" , "C Sharp" }; // Declaration of objects // of the JPanel class. JPanel p = new JPanel(); // Function to set size of JFrame. p.setSize( 600 , 400 ); // Function to set Layout of JFrame. p.setLayout( new GridLayout( 13 , 6 , 10 , 0 )); // for loop for ( int row = 0 ; row < 13 ; row++) { // Declaration of objects // of ButtonGroup class ButtonGroup bg = new ButtonGroup(); for ( int col = 0 ; col < 6 ; col++) { // If condition if (row == 0 ) { // add new Jlabel p.add( new JLabel(counts[col])); } else { // If condition if (col == 0 ) { // add new Jlabel p.add( new JLabel(categories[row - 1 ])); } else { form[row - 1 ][col - 1 ] = new JRadioButton(); // add form in ButtonGroup bg.add(form[row - 1 ][col - 1 ]); // add form in JFrame p.add(form[row - 1 ][col - 1 ]); } } } } // Declaration of objects // of scrollpane class. scrollpane = new JScrollPane(p); // to get content pane getContentPane().add(scrollpane, BorderLayout.CENTER); } // Main Method public static void main(String args[]) { new ScrollPanel(); } } |
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/javax/swing/ScrollPaneLayout.html