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