39 |
package javax.swing; |
package javax.swing; |
40 |
|
|
41 |
import java.awt.Component; |
import java.awt.Component; |
42 |
|
import java.awt.Point; |
43 |
|
|
44 |
|
|
45 |
/** |
/** |
90 |
*/ |
*/ |
91 |
protected Popup() |
protected Popup() |
92 |
{ |
{ |
93 |
|
// Nothing to do here. |
94 |
} |
} |
95 |
|
|
96 |
|
|
131 |
*/ |
*/ |
132 |
JWindow window; |
JWindow window; |
133 |
|
|
134 |
|
private Component contents; |
135 |
|
|
136 |
/** |
/** |
137 |
* Constructs a new <code>JWindowPopup</code> given its owner, |
* Constructs a new <code>JWindowPopup</code> given its owner, |
158 |
/* Checks whether contents is null. */ |
/* Checks whether contents is null. */ |
159 |
super(owner, contents, x, y); |
super(owner, contents, x, y); |
160 |
|
|
161 |
|
this.contents = contents; |
162 |
window = new JWindow(); |
window = new JWindow(); |
163 |
window.getRootPane().add(contents); |
window.getContentPane().add(contents); |
164 |
window.setLocation(x, y); |
window.setLocation(x, y); |
165 |
window.pack(); |
window.setFocusableWindowState(false); |
166 |
} |
} |
167 |
|
|
168 |
|
|
172 |
*/ |
*/ |
173 |
public void show() |
public void show() |
174 |
{ |
{ |
175 |
|
window.setSize(contents.getSize()); |
176 |
window.show(); |
window.show(); |
177 |
} |
} |
178 |
|
|
191 |
window.dispose(); |
window.dispose(); |
192 |
} |
} |
193 |
} |
} |
194 |
|
|
195 |
|
/** |
196 |
|
* A popup that displays itself within the JLayeredPane of a JRootPane of |
197 |
|
* the containment hierarchy of the owner component. |
198 |
|
* |
199 |
|
* @author Roman Kennke (kennke@aicas.com) |
200 |
|
*/ |
201 |
|
static class LightweightPopup extends Popup |
202 |
|
{ |
203 |
|
/** |
204 |
|
* The owner component for this popup. |
205 |
|
*/ |
206 |
|
Component owner; |
207 |
|
|
208 |
|
/** |
209 |
|
* The contents that should be shown. |
210 |
|
*/ |
211 |
|
Component contents; |
212 |
|
|
213 |
|
/** |
214 |
|
* The X location in screen coordinates. |
215 |
|
*/ |
216 |
|
int x; |
217 |
|
|
218 |
|
/** |
219 |
|
* The Y location in screen coordinates. |
220 |
|
*/ |
221 |
|
int y; |
222 |
|
|
223 |
|
/** |
224 |
|
* The panel that holds the content. |
225 |
|
*/ |
226 |
|
private JPanel panel; |
227 |
|
|
228 |
|
/** |
229 |
|
* Constructs a new <code>LightweightPopup</code> given its owner, |
230 |
|
* contents and the screen position where the popup |
231 |
|
* will appear. |
232 |
|
* |
233 |
|
* @param owner the component that should own the popup window; this |
234 |
|
* provides the JRootPane in which we place the popup window |
235 |
|
* |
236 |
|
* @param contents the contents that will be displayed inside |
237 |
|
* the <code>Popup</code>. |
238 |
|
* |
239 |
|
* @param x the horizontal position where the Popup will appear in screen |
240 |
|
* coordinates |
241 |
|
* |
242 |
|
* @param y the vertical position where the Popup will appear in screen |
243 |
|
* coordinates |
244 |
|
* |
245 |
|
* @throws IllegalArgumentException if <code>contents</code> |
246 |
|
* is <code>null</code>. |
247 |
|
*/ |
248 |
|
public LightweightPopup(Component owner, Component contents, int x, int y) |
249 |
|
{ |
250 |
|
super(owner, contents, x, y); |
251 |
|
this.owner = owner; |
252 |
|
this.contents = contents; |
253 |
|
this.x = x; |
254 |
|
this.y = y; |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
|
* Places the popup within the JLayeredPane of the owner component and |
259 |
|
* makes it visible. |
260 |
|
*/ |
261 |
|
public void show() |
262 |
|
{ |
263 |
|
JRootPane rootPane = SwingUtilities.getRootPane(owner); |
264 |
|
JLayeredPane layeredPane = rootPane.getLayeredPane(); |
265 |
|
// We insert a JPanel between the layered pane and the contents so we |
266 |
|
// can fiddle with the setLocation() method without disturbing a |
267 |
|
// JPopupMenu (which overrides setLocation in an unusual manner). |
268 |
|
if (panel == null) |
269 |
|
{ |
270 |
|
panel = new JPanel(); |
271 |
|
panel.setLayout(null); |
272 |
|
} |
273 |
|
panel.add(contents); |
274 |
|
panel.setSize(contents.getSize()); |
275 |
|
Point layeredPaneLoc = layeredPane.getLocationOnScreen(); |
276 |
|
panel.setLocation(x - layeredPaneLoc.x, y - layeredPaneLoc.y); |
277 |
|
layeredPane.add(panel, JLayeredPane.POPUP_LAYER); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
|
* Removes the popup from the JLayeredPane thus making it invisible. |
282 |
|
*/ |
283 |
|
public void hide() |
284 |
|
{ |
285 |
|
JRootPane rootPane = SwingUtilities.getRootPane(owner); |
286 |
|
JLayeredPane layeredPane = rootPane.getLayeredPane(); |
287 |
|
layeredPane.remove(panel); |
288 |
|
} |
289 |
|
} |
290 |
} |
} |