Java Swing | ScrollPaneLayout Class – GeeksforGeeks

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: 

  1. removeLayoutComponent(Component comp): Removes the specified component from the layout.
  2. getColumnHeader(): It returns the JViewport object that is the column header.
  3. getVerticalScrollBar(): Returns the JScrollBar object that handles the vertical scrolling.
  4. getHorizontalScrollBar(): Returns the JScrollBar object that handles the horizontal scrolling.
  5. addLayoutComponent(String st, Component c): Adds the specified component to the layout.
  6. getViewport(): Returns the JViewport object that displays the scrollable contents.
  7. 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.

Tóm Tắt

Java




import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JList;

import javax.swing.JScrollPane;

 

public class Geeks extends JFrame

 

{

     

    

    

    JScrollPane scrollpane;

 

    

    public Geeks()

    {

         

        

        

        super("JScrollPane Demonstration");

 

        

        setSize(300, 200);

 

        

        

        setDefaultCloseOperation(EXIT_ON_CLOSE);

 

        

        String categories[] = {"Geeks", "Language", "Java",

                               "Sudo Placement", "Python",

                               "CS Subject", "Operating System",

                               "Data Structure", "Algorithm",

                               "PHP language", "JAVASCRIPT",

                               "C Sharp" };

 

        

        JList list = new JList(categories);

 

        

        

        scrollpane = new JScrollPane(list);

 

        

        getContentPane().add(scrollpane, BorderLayout.CENTER);

    }

 

    

    public static void main(String args[])

    {

         

        

        Geeks sl = new Geeks();

 

        

        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




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;

 

public class ScrollPanel extends JFrame {

 

    

    

    JScrollPane scrollpane;

 

    

    public ScrollPanel()

    {

         

        

        

        super("JScrollPane Demonstration");

 

        

        setSize(300, 200);

 

        

        

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        init();

 

        

        

        setVisible(true);

    }

     

    

    public void init()

    {

         

        

        

        JRadioButton form[][] = new JRadioButton[12][5];

 

        

        String counts[] = {"", "1 star", "2 star",

                    "3 star", "4 star", "5 star"};

 

        

        String categories[] = {"Geeks", "Language", "Java",

                               "Sudo Placement", "Python",

                               "CS Subject", "Operating System",

                               "Data Structure", "Algorithm",

                               "PHP language", "JAVASCRIPT",

                               "C Sharp" };

 

        

        

        JPanel p = new JPanel();

 

        

        p.setSize(600, 400);

 

        

        p.setLayout(new GridLayout(13, 6, 10, 0));

 

        

        for (int row = 0; row < 13; row++) {

             

            

            

            ButtonGroup bg = new ButtonGroup();

 

            for (int col = 0; col < 6; col++)

            {

                 

                

                if (row == 0) {

 

                    

                    p.add(new JLabel(counts[col]));

                }

                else {

                    

                    if (col == 0)

                    {

 

                        

                        p.add(new JLabel(categories[row - 1]));

                    }

                     

                    else

                    {

                        form[row - 1][col - 1] = new JRadioButton();

 

                        

                        bg.add(form[row - 1][col - 1]);

 

                        

                        p.add(form[row - 1][col - 1]);

                    }

                }

            }

        }

 

        

        

        scrollpane = new JScrollPane(p);

 

        

        getContentPane().add(scrollpane, BorderLayout.CENTER);

    }

 

    

    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
 

My Personal Notes

arrow_drop_up