ScrollPaneDemo.java revision 13978:1993af50385d
1/*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package com.sun.swingset3.demos.scrollpane;
24
25import java.awt.*;
26import javax.swing.*;
27
28import com.sun.swingset3.DemoProperties;
29import com.sun.swingset3.demos.ResourceManager;
30
31/**
32 * Scroll Pane Demo
33 *
34 * @version 1.9 11/17/05
35 * @author Jeff Dinkins
36 */
37@DemoProperties(
38        value = "JScrollPane Demo",
39        category = "Containers",
40        description = "Demonstrates JScrollPane, a container for scrolling contents within a view port",
41        sourceFiles = {
42            "com/sun/swingset3/demos/scrollpane/ScrollPaneDemo.java",
43            "com/sun/swingset3/demos/ResourceManager.java",
44            "com/sun/swingset3/demos/scrollpane/resources/ScrollPaneDemo.properties",
45            "com/sun/swingset3/demos/scrollpane/resources/images/colheader.jpg",
46            "com/sun/swingset3/demos/scrollpane/resources/images/COPYRIGHT",
47            "com/sun/swingset3/demos/scrollpane/resources/images/crayons.jpg",
48            "com/sun/swingset3/demos/scrollpane/resources/images/lowerleft.jpg",
49            "com/sun/swingset3/demos/scrollpane/resources/images/rowheader.jpg",
50            "com/sun/swingset3/demos/scrollpane/resources/images/ScrollPaneDemo.gif",
51            "com/sun/swingset3/demos/scrollpane/resources/images/upperleft.jpg",
52            "com/sun/swingset3/demos/scrollpane/resources/images/upperright.jpg"}
53)
54public class ScrollPaneDemo extends JPanel {
55
56    private final ResourceManager resourceManager = new ResourceManager(this.getClass());
57    public static final String DEMO_TITLE = ScrollPaneDemo.class.getAnnotation(DemoProperties.class).value();
58
59    /**
60     * main method allows us to run as a standalone demo.
61     *
62     * @param args
63     */
64    public static void main(String[] args) {
65        JFrame frame = new JFrame(DEMO_TITLE);
66
67        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
68        frame.getContentPane().add(new ScrollPaneDemo());
69        frame.setPreferredSize(new Dimension(800, 600));
70        frame.pack();
71        frame.setLocationRelativeTo(null);
72        frame.setVisible(true);
73    }
74
75    /**
76     * ScrollPaneDemo Constructor
77     */
78    public ScrollPaneDemo() {
79        setLayout(new BorderLayout());
80
81        ImageIcon crayons = resourceManager.createImageIcon("crayons.jpg",
82                resourceManager.getString("ScrollPaneDemo.crayons"));
83        add(new ImageScroller(crayons), BorderLayout.CENTER);
84    }
85
86    /**
87     * ScrollPane class that demonstrates how to set the various column and row
88     * headers and corners.
89     */
90    private class ImageScroller extends JScrollPane {
91
92        public ImageScroller(Icon icon) {
93            super();
94
95            // Panel to hold the icon image
96            JPanel p = new JPanel(new BorderLayout());
97            p.add(new JLabel(icon), BorderLayout.CENTER);
98            getViewport().add(p);
99
100            // Create and add a column header to the scrollpane
101            JLabel colHeader = new JLabel(
102                    resourceManager.createImageIcon("colheader.jpg", resourceManager.getString("ScrollPaneDemo.colheader")));
103            setColumnHeaderView(colHeader);
104
105            // Create and add a row header to the scrollpane
106            JLabel rowHeaderLabel = new JLabel(
107                    resourceManager.createImageIcon("rowheader.jpg", resourceManager.getString("ScrollPaneDemo.rowheader")));
108            setRowHeaderView(rowHeaderLabel);
109
110            // Create and add the upper left corner
111            JLabel cornerUL = new JLabel(
112                    resourceManager.createImageIcon("upperleft.jpg", resourceManager.getString("ScrollPaneDemo.upperleft")));
113            setCorner(UPPER_LEFT_CORNER, cornerUL);
114
115            // Create and add the upper right corner
116            JLabel cornerUR = new JLabel(
117                    resourceManager.createImageIcon("upperright.jpg", resourceManager.getString("ScrollPaneDemo.upperright")));
118            setCorner(UPPER_RIGHT_CORNER, cornerUR);
119
120            // Create and add the lower left corner
121            JLabel cornerLL = new JLabel(
122                    resourceManager.createImageIcon("lowerleft.jpg", resourceManager.getString("ScrollPaneDemo.lowerleft")));
123            setCorner(LOWER_LEFT_CORNER, cornerLL);
124
125            JScrollBar vsb = getVerticalScrollBar();
126            JScrollBar hsb = getHorizontalScrollBar();
127
128            vsb.setValue(icon.getIconHeight());
129            hsb.setValue(icon.getIconWidth() / 10);
130        }
131    }
132
133}
134