jdk/test/sanity/client/lib/SwingSet3/src/com/sun/swingset3/demos/button/ButtonDemo.java
changeset 36744 a00905527ec2
child 37574 906cb708a629
equal deleted inserted replaced
36743:bdc3f1b79fb7 36744:a00905527ec2
       
     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  */
       
    23 package com.sun.swingset3.demos.button;
       
    24 
       
    25 import java.awt.Color;
       
    26 import java.awt.FlowLayout;
       
    27 import java.awt.GridLayout;
       
    28 import java.awt.event.ActionEvent;
       
    29 import java.net.URISyntaxException;
       
    30 import javax.swing.BorderFactory;
       
    31 import javax.swing.ImageIcon;
       
    32 import javax.swing.JButton;
       
    33 import javax.swing.JFrame;
       
    34 import javax.swing.JPanel;
       
    35 import javax.swing.SwingUtilities;
       
    36 
       
    37 import com.sun.swingset3.DemoProperties;
       
    38 import com.sun.swingset3.demos.JHyperlink;
       
    39 
       
    40 /**
       
    41  *
       
    42  * @author aim
       
    43  */
       
    44 @DemoProperties(
       
    45         value = "JButton Demo",
       
    46         category = "Controls",
       
    47         description = "Demonstrates the many uses of JButton, Swing's push button component.",
       
    48         sourceFiles = {
       
    49             "com/sun/swingset3/demos/button/ButtonDemo.java",
       
    50             "com/sun/swingset3/demos/JHyperlink.java",
       
    51             "com/sun/swingset3/demos/button/resources/ButtonDemo.html",
       
    52             "com/sun/swingset3/demos/button/resources/images/blogs.png",
       
    53             "com/sun/swingset3/demos/button/resources/images/ButtonDemo.gif",
       
    54             "com/sun/swingset3/demos/button/resources/images/document-print.png",
       
    55             "com/sun/swingset3/demos/button/resources/images/earth_day.gif",
       
    56             "com/sun/swingset3/demos/button/resources/images/earth_night.gif",
       
    57             "com/sun/swingset3/demos/button/resources/images/edit-find.png",
       
    58             "com/sun/swingset3/demos/button/resources/images/redbutton.png",
       
    59             "com/sun/swingset3/demos/button/resources/images/redbutton_dark.png",
       
    60             "com/sun/swingset3/demos/button/resources/images/redbutton_glow.png"
       
    61         }
       
    62 )
       
    63 public final class ButtonDemo extends JPanel {
       
    64 
       
    65     public static final String DEMO_TITLE = ButtonDemo.class.getAnnotation(DemoProperties.class).value();
       
    66     public static final String DO_IT_AGAIN = "Do it again";
       
    67     public static final String DO_IT = "Do it";
       
    68     public static final String BUTTON_WITH_TEXT_AND_IMAGE = "button with text and image";
       
    69     public static final String BUTTON_WITH_BACKGROUND_COLOR = "button with background color";
       
    70     public static final String GO = "Go";
       
    71     public static final String FIND = "Find";
       
    72     public static final String IMAGE_BUTTON = "image button";
       
    73     public static final String SIMPLE_BUTTON = "simple button";
       
    74     public static final String GET_MORE_INFO = "Get More Info";
       
    75     public static final String JAVA_BLOGS_URL = "https://blogs.oracle.com/java/";
       
    76     public static final String JAVA_SE_URL = "http://www.oracle.com/technetwork/java/javase/overview/index.html";
       
    77     public static final String BUTTON_WITH_ROLLOVER_IMAGE = "button with rollover image";
       
    78     public static final String BUTTON_WITH_NO_BORDER = "button with no border";
       
    79     public static final String CONNECT = "Connect";
       
    80 
       
    81     public ButtonDemo() {
       
    82         setToolTipText("Demonstrates JButton, Swing's push button component.");
       
    83         initComponents();
       
    84         setOpaque(false);
       
    85     }
       
    86 
       
    87     protected void initComponents() {
       
    88         setLayout(new GridLayout(0, 1));
       
    89 
       
    90         add(createSimpleButtonPanel());
       
    91         add(createCreativeButtonPanel());
       
    92     }
       
    93 
       
    94     protected JPanel createSimpleButtonPanel() {
       
    95         JPanel panel = new JPanel();
       
    96         panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 8));
       
    97         panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),
       
    98                 "Simple Buttons"));
       
    99 
       
   100         //<snip>Create simple button
       
   101         final JButton simpleButton = new JButton(DO_IT);
       
   102         simpleButton.setToolTipText(SIMPLE_BUTTON);
       
   103         //</snip>
       
   104         //<snip>Add action listener using anonymous inner class
       
   105         // This style is useful when the action code is tied to a
       
   106         // single button instance and it's useful for simplicity
       
   107         // sake to keep the action code located near the button.
       
   108         // More global application actions should be implemented
       
   109         // using Action classes instead.
       
   110         simpleButton.addActionListener((ActionEvent event) -> {
       
   111             simpleButton.setText(DO_IT_AGAIN);
       
   112             // Need to force toplevel to relayout to accommodate new button size
       
   113             SwingUtilities.getWindowAncestor(simpleButton).validate();
       
   114         });
       
   115         //</snip>
       
   116         simpleButton.putClientProperty("snippetKey", "Create simple button");
       
   117         panel.add(simpleButton);
       
   118 
       
   119         //<snip>Create image button
       
   120         // Image is from the Java Look and Feel Graphics Repository
       
   121         JButton button = new JButton(new ImageIcon(getClass().
       
   122                 getResource("resources/images/document-print.png")));
       
   123         button.setToolTipText(IMAGE_BUTTON);
       
   124         //</snip>
       
   125         button.putClientProperty("snippetKey", "Create image button");
       
   126         panel.add(button);
       
   127 
       
   128         //<snip>Create button with text and image
       
   129         // Image is from the Java Look and Feel Graphics Repository
       
   130         button = new JButton(FIND,
       
   131                 new ImageIcon(getClass().
       
   132                         getResource("resources/images/edit-find.png")));
       
   133         button.setToolTipText(BUTTON_WITH_TEXT_AND_IMAGE);
       
   134         button.setHorizontalTextPosition(JButton.LEADING);
       
   135         button.setIconTextGap(6);
       
   136         //</snip>
       
   137         button.putClientProperty("snippetKey", "Create button with text and image");
       
   138         panel.add(button);
       
   139 
       
   140         //<snip>Create button with background color
       
   141         button = new JButton(GO);
       
   142         button.setBackground(Color.green);
       
   143         button.setContentAreaFilled(true);
       
   144         button.setOpaque(false);
       
   145         button.setToolTipText(BUTTON_WITH_BACKGROUND_COLOR);
       
   146         //</snip>
       
   147         button.putClientProperty("snippetKey", "Create button with background color");
       
   148         panel.add(button);
       
   149 
       
   150         return panel;
       
   151     }
       
   152 
       
   153     protected JPanel createCreativeButtonPanel() {
       
   154         JPanel panel = new JPanel();
       
   155         panel.setLayout(new FlowLayout(FlowLayout.CENTER, 16, 8));
       
   156         panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),
       
   157                 "More Interesting Buttons"));
       
   158 
       
   159         //<snip>Create button with no border
       
   160         JButton button = new JButton();
       
   161         button.setText(CONNECT);
       
   162         button.setIcon(new ImageIcon(getClass().getResource("resources/images/earth_day.gif")));
       
   163         button.setPressedIcon(new ImageIcon(getClass().getResource("resources/images/earth_night.gif")));
       
   164         button.setBorderPainted(false);
       
   165         button.setContentAreaFilled(false);
       
   166         button.setVerticalTextPosition(JButton.BOTTOM);
       
   167         button.setHorizontalTextPosition(JButton.CENTER);
       
   168         button.setIconTextGap(0);
       
   169         button.setToolTipText(BUTTON_WITH_NO_BORDER);
       
   170         //</snip>
       
   171         button.putClientProperty("snippetKey", "Create button with no border");
       
   172         panel.add(button);
       
   173 
       
   174         //<snip>Create image button with rollover image
       
   175         button = new JButton();
       
   176         button.setBorderPainted(false);
       
   177         button.setContentAreaFilled(false);
       
   178         button.setIcon(new ImageIcon(getClass().getResource("resources/images/redbutton.png")));
       
   179         button.setRolloverEnabled(true);
       
   180         button.setRolloverIcon(new ImageIcon(getClass().getResource("resources/images/redbutton_glow.png")));
       
   181         button.setPressedIcon(new ImageIcon(getClass().getResource("resources/images/redbutton_dark.png")));
       
   182         button.setToolTipText(BUTTON_WITH_ROLLOVER_IMAGE);
       
   183         //</snip>
       
   184         button.putClientProperty("snippetKey", "Create image button with rollover image");
       
   185         panel.add(button);
       
   186 
       
   187         //<snip>Create HTML hyperlink
       
   188         JHyperlink hyperlink;
       
   189         try {
       
   190             hyperlink = new JHyperlink(GET_MORE_INFO, JAVA_SE_URL);
       
   191         } catch (URISyntaxException use) {
       
   192             use.printStackTrace();
       
   193             hyperlink = new JHyperlink(GET_MORE_INFO);
       
   194         }
       
   195         //</snip>
       
   196         hyperlink.putClientProperty("snippetKey", "Create HTML hyperlink");
       
   197         panel.add(hyperlink);
       
   198 
       
   199         //<snip>Create HTML image hyperlink
       
   200         try {
       
   201             hyperlink = new JHyperlink(
       
   202                     new ImageIcon(getClass().getResource("resources/images/blogs.png")), JAVA_BLOGS_URL);
       
   203         } catch (URISyntaxException use) {
       
   204             use.printStackTrace();
       
   205         }
       
   206         //</snip>
       
   207         button.putClientProperty("snippetKey", "Create HTML image hyperlink");
       
   208         panel.add(hyperlink);
       
   209 
       
   210         return panel;
       
   211     }
       
   212 
       
   213     public static void main(String args[]) {
       
   214         final ButtonDemo buttonDemo = new ButtonDemo();
       
   215 
       
   216         javax.swing.SwingUtilities.invokeLater(() -> {
       
   217             JFrame frame = new JFrame(DEMO_TITLE);
       
   218             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
   219             frame.add(buttonDemo);
       
   220             frame.pack();
       
   221             frame.setVisible(true);
       
   222         });
       
   223     }
       
   224 }