JScrollPane in Java with Example using NetBeans

JScrollPane in java:

The javax.swing.JScrollPane class is a container that is equipped with a scrollbar if the content is too large to be able to display it completely. Scrollbars are probably familiar to you from surfing the Internet. This is the scroll bar that should still be present on the right side of your browser window so you can read the full text by scrolling down.

In addition to the usual parameterless default constructor, the JScrollPane class also has the following constructors:

JScrollPane Constructors:

constructordescription
JScrollPane(Component view)The component that is to be displayed within the JScrollPanes is passed to the constructor.
JScrollPane(Component view, int vsbPolicy, int hsbPolicy)In addition to the component to be displayed, two additional parameters are passed here with which you can control the visibility of the scrollbars. For this there is the  ScrollPaneConstants interface with the corresponding constants, which we will explain in the next table.
JScrollPane(int vsbPolicy, int hsbPolicy)As above, but without the parameter for the component.

The ScrollPaneConstants interface provides the following constants for the  vsbPolicy and hsbPolicy parameters:

constantexplanation
ScrollPaneConstants.

HORIZONTAL_SCROLLBAR_AS_NEEDED

Horizontal scrollbar is only shown when it is needed.
ScrollPaneConstants.

HORIZONTAL_SCROLLBAR_NEVER

Horizontal scrollbar is never displayed.
ScrollPaneConstants.

HORIZONTAL_SCROLLBAR_ALWAYS

Horizontal scrollbar is always displayed.
ScrollPaneConstants.

VERTICAL_SCROLLBAR_AS_NEEDED

Vertical scrollbar only appears when needed.
ScrollPaneConstants.

VERTICAL_SCROLLBAR_NEVER

Vertical scrollbar is never displayed.
ScrollPaneConstants.

VERTICAL_SCROLLBAR_ALWAYS

Vertical scrollbar is always displayed.


 

Example: how to use JScrollPane in java using NetBeans:

package com.mycompany.guiexamples;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.ScrollPaneConstants;

/**

*

* @author Fawadkhan

*/

public class JScrollPaneExample {

    // main method

    public static void main(String[] args) {

        

        JDialog myJDialog = new JDialog();

        myJDialog.setTitle("JScrollPane Testing Example");

        myJDialog.setSize(450, 300);

        // JPanel is created

        JPanel panel = new JPanel();

        // Our JPanel long text

        panel.add(new JLabel("Lorem ipsum is placeholder text "

                + "commonly used in the graphic, "

                + "print, and publishing industries for "

                + "previewing layouts and visual mockups."));

        // JScrollPane is created; our JPanel is added directly via the

        // constructor

        JScrollPane scrollPane = new JScrollPane(panel,

                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,

                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        // JScrollPane is added to the dialog

        myJDialog.add(scrollPane);

        // display dialog

        myJDialog.setVisible(true);

    }

}

First, I create a JPanel with a long text, which is wider than the dialog window. In order to be able to read the text completely, we need a scrollbar. So I create a JScrollPane object using the constructor as you can see in the constructor table above. This gets our previously created JPanel, which is to be displayed in the JScrollPane. In addition, we use the constants to specify that the JScrollPane should only have a vertical and horizontal scrollbar when they are required. In our example, only a horizontal scrollbar is needed to see the complete JLabel:

JScrollPane

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值