Java JScrollPane
is a Swing component that provides a scrollable view of a component. It is used when the content to be displayed in a component exceeds the available size of the component. The JScrollPane
can be added to any Swing component, such as JTextArea
, JTable
, JList
, or even a custom component.
The JScrollPane
has two components: a viewport and scroll bars. The viewport is the component that displays the content and the scroll bars are used to scroll the content within the viewport. The scroll bars appear only when the content is larger than the viewport. The JScrollPane
also provides various customization options for scroll bars, such as orientation, visibility, and appearance.
To use a JScrollPane
, you first need to create the component that will be added to it. For example, to add a JTextArea
to a JScrollPane
, you can do the following:
JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea);
This will create a JScrollPane
that displays the JTextArea
and provides scroll bars when the content exceeds the available size of the viewport. You can then add the JScrollPane
to a container, such as a JPanel
or a JFrame
.
JPanel panel = new JPanel(); panel.add(scrollPane);
In addition to the basic functionality, you can customize the JScrollPane
by setting various properties, such as the scroll bar policy, the scroll bar appearance, and the viewport size. For example, to set the horizontal scroll bar policy to always display and the vertical scroll bar policy to as needed, you can do the following:
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Overall, the JScrollPane
provides a simple and convenient way to add scrollable views to Swing components.
The Java JScrollPane
class provides several constructors to create instances of the JScrollPane
class with different parameters. Here are the constructors provided by the JScrollPane
class:
JScrollPane()
– Creates aJScrollPane
with no viewport, no vertical scroll bar, and no horizontal scroll bar.JScrollPane(Component view)
– Creates aJScrollPane
with the specified component as the viewport, with default scroll bar policies.JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
– Creates aJScrollPane
with the specified component as the viewport, with the specified vertical and horizontal scroll bar policies.JScrollPane(int vsbPolicy, int hsbPolicy)
– Creates aJScrollPane
with no viewport, with the specified vertical and horizontal scroll bar policies.JScrollPane(Component view, int vsbPolicy, int hsbPolicy, boolean hasWheel)
– Creates aJScrollPane
with the specified component as the viewport, with the specified vertical and horizontal scroll bar policies, and whether or not scrolling with the mouse wheel is enabled.JScrollPane(Component view, int vsbPolicy, int hsbPolicy, JScrollBar vsb, JScrollBar hsb)
– Creates aJScrollPane
with the specified component as the viewport, with the specified vertical and horizontal scroll bars.JScrollPane(Component view, int vsbPolicy, int hsbPolicy, JViewport viewport)
– Creates aJScrollPane
with the specified component as the viewport, with the specified vertical and horizontal scroll bar policies, and the specified viewport.
The vsbPolicy
and hsbPolicy
parameters are integers that specify the vertical and horizontal scroll bar policies respectively. These parameters can be set to one of the following constants defined in the ScrollPaneConstants
interface:
VERTICAL_SCROLLBAR_AS_NEEDED
: the scroll bar appears only when the content exceeds the viewport size.VERTICAL_SCROLLBAR_NEVER
: the vertical scroll bar is not shown.VERTICAL_SCROLLBAR_ALWAYS
: the vertical scroll bar is always shown.HORIZONTAL_SCROLLBAR_AS_NEEDED
: the horizontal scroll bar appears only when the content exceeds the viewport size.HORIZONTAL_SCROLLBAR_NEVER
: the horizontal scroll bar is not shown.HORIZONTAL_SCROLLBAR_ALWAYS
: the horizontal scroll bar is always shown.
The hasWheel
parameter specifies whether or not scrolling with the mouse wheel is enabled.
The vsb
and hsb
parameters are instances of the JScrollBar
class that represent the vertical and horizontal scroll bars respectively.
The viewport
parameter is an instance of the JViewport
class that represents the viewport to be used for the JScrollPane
.
Overall, these constructors provide a range of options to create instances of the JScrollPane
class with different configurations.
Useful Methods:
The JScrollPane
class in Java provides many useful methods to work with scroll panes. Here are some of the most commonly used methods:
setViewportView(Component view)
– Sets the component that will be displayed in the viewport of theJScrollPane
.getViewport()
– Returns theJViewport
object that is used as the viewport for theJScrollPane
.setVerticalScrollBarPolicy(int policy)
– Sets the policy for the vertical scroll bar.setHorizontalScrollBarPolicy(int policy)
– Sets the policy for the horizontal scroll bar.setViewportBorder(Border border)
– Sets the border of the viewport.setWheelScrollingEnabled(boolean enabled)
– Enables or disables scrolling with the mouse wheel.getVerticalScrollBar()
– Returns the vertical scroll bar of theJScrollPane
.getHorizontalScrollBar()
– Returns the horizontal scroll bar of theJScrollPane
.getViewportBorderBounds()
– Returns the bounds of the viewport border.setViewportViewPosition(Point p)
– Sets the position of the viewport to the specified point.getViewportSize()
– Returns the size of the viewport.scrollRectToVisible(Rectangle contentRect)
– Scrolls the viewport so that the specified rectangle is visible.
These methods provide a range of functionality for working with JScrollPane
components in Java. By using these methods, you can customize the behavior and appearance of scroll panes, as well as control the content that is displayed within them.
JScrollPane Example:
Here’s an example of using JScrollPane
in Java Swing to display a JTextArea
that is larger than the available space in a JPanel
:
import javax.swing.*; import java.awt.*; public class ScrollPaneExample extends JFrame { private JTextArea textArea; public ScrollPaneExample() { // Create a new JTextArea that contains some long text textArea = new JTextArea("This is a very long piece of text that will need scrolling."); // Create a new JScrollPane that contains the JTextArea JScrollPane scrollPane = new JScrollPane(textArea); // Set the preferred size of the JScrollPane scrollPane.setPreferredSize(new Dimension(200, 200)); // Create a new JPanel to hold the JScrollPane JPanel panel = new JPanel(); // Add the JScrollPane to the JPanel panel.add(scrollPane); // Add the JPanel to the JFrame getContentPane().add(panel); // Set the title, size, and close operation of the JFrame setTitle("Scroll Pane Example"); setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Make the JFrame visible setVisible(true); } public static void main(String[] args) { new ScrollPaneExample(); } }
In this example, we create a JTextArea
with some long text, and then we create a JScrollPane
to contain the text area. We set the preferred size of the scroll pane to be 200×200 pixels, and then add the scroll pane to a JPanel
. Finally, we add the panel to a JFrame
, and make the frame visible.
When we run the program, the JTextArea
is displayed inside the JScrollPane
, and if the text exceeds the available space, scroll bars will automatically appear to allow the user to scroll the text.