A JScrollPane provides scroll functions for other elements.
The following code demonstrates how to create a JScrollPane with different options:
To create a JScrollPane with no component as its viewport and with default scrollbars policy
JScrollPane sp1 = new JScrollPane();
To 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);
To 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);
The viewport of a JScrollPane keeps the reference to the component we add to the JScrollPane. You get the reference of the component in a JScrollPane by querying its viewport as shown:
JViewport vp = sp3.getViewport(); JTextArea comments1 = (JTextArea)vp.getView();
To add a component to JScrollPane's viewport:
sp3.setViewportView(new JTextPane());
To place a component in one of the corners of the JScrollPane, call setCorner(String key, Component corner) key is
import java.awt.BorderLayout; import java.awt.GridLayout; /*from ww w .j a v a 2s . co m*/ import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; import javax.swing.SwingUtilities; public class Main { public static void main(String[] a) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new JScrollPaneDemo()); f.setSize(500, 500); f.setVisible(true); } } class JScrollPaneDemo extends JPanel { public void init() { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { makeGUI(); } }); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { setLayout(new BorderLayout()); JPanel jp = new JPanel(); jp.setLayout(new GridLayout(20, 20)); int b = 0; for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { jp.add(new JButton("Button " + b)); ++b; } } int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane jsp = new JScrollPane(jp, v, h); add(jsp, BorderLayout.CENTER); } }
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; /*from w ww . j a va 2 s .c o m*/ import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.border.LineBorder; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Label"); label.setPreferredSize(new Dimension(1000, 1000)); JScrollPane jScrollPane = new JScrollPane(label); JButton jButton1 = new JButton(); jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane.setViewportBorder(new LineBorder(Color.RED)); jScrollPane.getViewport().add(jButton1, null); frame.add(jScrollPane, BorderLayout.CENTER); frame.setSize(400, 150); frame.setVisible(true); } }
import java.awt.event.*; import javax.swing.*; /*from w w w. j a v a2s .c om*/ import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollBar; import javax.swing.JScrollPane; class JScrollPaneToTopAction implements ActionListener { JScrollPane scrollPane; public JScrollPaneToTopAction(JScrollPane scrollPane) { if (scrollPane == null) { throw new IllegalArgumentException( "JScrollPaneToTopAction: null JScrollPane"); } this.scrollPane = scrollPane; } public void actionPerformed(ActionEvent actionEvent) { JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar(); JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar(); verticalScrollBar.setValue(verticalScrollBar.getMinimum()); horizontalScrollBar.setValue(horizontalScrollBar.getMinimum()); } } public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Label"); label.setPreferredSize(new Dimension(1000, 1000)); JScrollPane jScrollPane = new JScrollPane(label); JButton bn = new JButton("Move"); bn.addActionListener(new JScrollPaneToTopAction(jScrollPane)); frame.add(bn, BorderLayout.SOUTH); frame.add(jScrollPane, BorderLayout.CENTER); frame.setSize(400, 150); frame.setVisible(true); } }
Listening for Scrollbar Value Changes in a JScrollPane Container
import java.awt.Adjustable; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; // www . j ava2 s .co m import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main { public static void main(String[] argv) throws Exception { JTextArea textArea = new JTextArea(); JScrollPane pane = new JScrollPane(textArea); // Listen for value changes in the scroll pane's scrollbars AdjustmentListener listener = new MyAdjustmentListener(); pane.getHorizontalScrollBar().addAdjustmentListener(listener); pane.getVerticalScrollBar().addAdjustmentListener(listener); } } class MyAdjustmentListener implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent evt) { Adjustable source = evt.getAdjustable(); if (evt.getValueIsAdjusting()) { return; } int orient = source.getOrientation(); if (orient == Adjustable.HORIZONTAL) { System.out.println("from horizontal scrollbar"); } else { System.out.println("from vertical scrollbar"); } int type = evt.getAdjustmentType(); switch (type) { case AdjustmentEvent.UNIT_INCREMENT: System.out.println("Scrollbar was increased by one unit"); break; case AdjustmentEvent.UNIT_DECREMENT: System.out.println("Scrollbar was decreased by one unit"); break; case AdjustmentEvent.BLOCK_INCREMENT: System.out.println("Scrollbar was increased by one block"); break; case AdjustmentEvent.BLOCK_DECREMENT: System.out.println("Scrollbar was decreased by one block"); break; case AdjustmentEvent.TRACK: System.out.println("The knob on the scrollbar was dragged"); break; } int value = evt.getValue(); } }