Wednesday 28 January 2015

Filled Under:

Tutorial On Scroll Pane In Java



When a screen is limited then we need a scroll pane to show components that is large as I have added scroll pane to a text area in this example to shows how to add component in a jscroll pane.Because a only limited part of text will be visible to a user to make visible whole text in a text area we have to use jscroll pane.

SCROLL PANES IN JAVA:

A scroll pane is a component that presents a rectangular area in which a component may be viewed.some its constructor are shown below
JScrollPane(Component comp)
JScrollPane(int vsb,int hsb)
JScrollPane(Component comp,int vsb,int hsb)
JScrollPane()

Here component specifies the component that is to be added as in our example textArea is an component.vsb is a vertical scroll bar policy while hsb is horizontal scroll bar policy.


SCROLL BAR POLICIES:

There are two scroll bar in a scroll pane some time we always need them ,sometime we never need them and sometime we need one or both of them on some situations this behaviour can be controlled by following policies.


VERTICAL_SCROLLBAR_AS_NEEDED
VERTICAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_NEVER
HORIZONTAL_SCROLLBAR_AS_NEEDED
HORIZONTAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_NEVER


CODING:

import javax.swing.*;
import java.awt.*;

class myjframe1 extends JFrame
 {

public myjframe1()
 {

setTitle("Simple example");
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);    
JTextAre textArea = new JTextArea(10,30);
setLayout(new FlowLayout(FlowLayout.CENTER));
JScrollPane js = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(js);
}
}
class text
{
public static void main(String args[])
{
myjframe1 x= new myjframe1();
}
}


OUTPUT:



0 comments:

Post a Comment