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