Create JScrollPane - Java Swing

Java examples for Swing:JScrollPane

Introduction

Create a JScrollPane with no component as its viewport and with default scrollbars policy as "As Needed"

JScrollPane sp1 = new JScrollPane();

Create a JScrollPane with a JTextArea as its viewport and with default scrollbars policy as "As Needed"

JTextArea description = new JTextArea(10, 60);
JScrollPane sp2 = new JScrollPane(description);

Create a JScrollPane with a JTextArea as its viewport and both scrollbars policy set to "show always"

JTextArea comments = new JTextArea(10, 60);
JScrollPane sp3 = new JScrollPane(comments,
                                  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                  JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

Get the reference to the viewport of the JScrollPane sp3

JViewport vp = sp3.getViewport();

Get the reference to the comments JTextArea added to the JScrollPane, sp3, using its viewport reference

JTextArea comments1 = (JTextArea)vp.getView();

add a component to its viewport later using its setViewportView() method.

sp3.setViewportView(new JTextPane());

Related Tutorials