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.window;
24 
25 import java.awt.*;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import javax.swing.*;
29 import javax.swing.border.EmptyBorder;
30 import javax.swing.border.LineBorder;
31 
32 import com.sun.swingset3.DemoProperties;
33 import com.sun.swingset3.demos.DemoUtilities;
34 import java.lang.reflect.InvocationTargetException;
35 
36 /**
37  * @author aim
38  */
39 @DemoProperties(
40         value = "JWindow Demo",
41         category = "Toplevel Containers",
42         description = "Demonstrates JWindow, a toplevel container with no system border.",
43         sourceFiles = {
44             "com/sun/swingset3/demos/window/WindowDemo.java",
45             "com/sun/swingset3/demos/DemoUtilities.java",
46             "com/sun/swingset3/demos/window/resources/WindowDemo.html",
47             "com/sun/swingset3/demos/window/resources/images/WindowDemo.gif"
48         }
49 )
50 public final class WindowDemo extends JPanel {
51 
52     public static final String SHOW_J_WINDOW = "Show JWindow...";
53     public static final String I_HAVE_NO_SYSTEM_BORDER = "I have no system border.";
54 
55     private JWindow window;
56 
57     private JComponent windowSpaceholder;
58 
WindowDemo()59     public WindowDemo() {
60         initComponents();
61     }
62 
initComponents()63     protected void initComponents() {
64         window = createWindow();
65 
66         setLayout(new BorderLayout());
67         add(createControlPanel(), BorderLayout.WEST);
68         windowSpaceholder = createWindowSpaceholder(window);
69         add(windowSpaceholder, BorderLayout.CENTER);
70     }
71 
createControlPanel()72     protected JComponent createControlPanel() {
73         Box controlPanel = Box.createVerticalBox();
74         controlPanel.setBorder(new EmptyBorder(8, 8, 8, 8));
75 
76         // Create button to control visibility of frame
77         JButton showButton = new JButton(SHOW_J_WINDOW);
78         showButton.addActionListener(new ShowActionListener());
79         controlPanel.add(showButton);
80 
81         return controlPanel;
82     }
83 
createWindowSpaceholder(JWindow window)84     private static JComponent createWindowSpaceholder(JWindow window) {
85         JPanel windowPlaceholder = new JPanel();
86         Dimension prefSize = window.getPreferredSize();
87         prefSize.width += 12;
88         prefSize.height += 12;
89         windowPlaceholder.setPreferredSize(prefSize);
90 
91         return windowPlaceholder;
92     }
93 
createWindow()94     private static JWindow createWindow() {
95 
96         //<snip>Create window
97         JWindow window = new JWindow();
98         //</snip>
99 
100         //<snip>Add a border to the window
101         window.getRootPane().setBorder(new LineBorder(Color.BLACK, 1));
102         //</snip>
103 
104         //<snip>Add window's content
105         JLabel label = new JLabel(I_HAVE_NO_SYSTEM_BORDER);
106         label.setHorizontalAlignment(JLabel.CENTER);
107         label.setPreferredSize(new Dimension(250, 200));
108         window.add(label);
109         //</snip>
110 
111         //<snip>Initialize window's size
112         // which will shrink-to-fit its contents
113         window.pack();
114         //</snip>
115 
116         return window;
117     }
118 
start()119     public void start() {
120         DemoUtilities.setToplevelLocation(window, windowSpaceholder, SwingConstants.CENTER);
121         showWindow();
122     }
123 
stop()124     public void stop() {
125         //<snip>Hide window
126         window.setVisible(false);
127         //</snip>
128     }
129 
showWindow()130     public void showWindow() {
131         //<snip>Show window
132         // if window already visible, then bring to the front
133         if (window.isShowing()) {
134             window.toFront();
135         } else {
136             window.setVisible(true);
137         }
138         //</snip>
139     }
140 
141     private class ShowActionListener implements ActionListener {
142 
143         @Override
actionPerformed(ActionEvent actionEvent)144         public void actionPerformed(ActionEvent actionEvent) {
145             showWindow();
146         }
147     }
148 
main(String args[])149     public static void main(String args[]) throws InterruptedException, InvocationTargetException {
150         EventQueue.invokeAndWait(() -> {
151             JFrame frame = new JFrame();
152             WindowDemo demo = new WindowDemo();
153             frame.add(demo);
154             frame.pack();
155             frame.setVisible(true);
156             demo.start();
157         });
158     }
159 }
160