My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package net.hanjava.swing;

import java.awt.Window;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;

import sun.awt.AppContext;

public class Util {
private static boolean swingInitializd = false;

// do not instanciate
private Util() {
}

public static void show(JComponent target, int x, int y, int w, int h) {
ensureSwingInit();
JFrame frame = new JFrame(target.getClass().toString());
frame.setContentPane(target);
frame.setBounds(x, y, w, h);
frame.setVisible(true);
}

public static void show(JComponent target, int x, int y) {
ensureSwingInit();
JFrame frame = new JFrame(target.getClass().toString());
frame.setContentPane(target);
frame.pack();
frame.setLocation(x, y);
frame.setVisible(true);
}

public static void show(JComponent target) {
ensureSwingInit();
JFrame frame = new JFrame(target.getClass().toString());
frame.setContentPane(target);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void ensureSwingInit() {
if (swingInitializd)
return;
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
swingInitializd = true;
}

/**
* get all instances of <code>java.awt.Window</code> in current VM. only
* works for Sun JVM 1.6+
*/
private static List<Window> getAllWindowsFromStatic() throws Exception {
Method method = Window.class.getDeclaredMethod("getAllWindows",
new Class[0]);
method.setAccessible(true);
Object result = method.invoke(null, new Object[] {});
return (List<Window>) result;
}

/**
* returns all instances of <code>java.awt.Window</code> in caller
* thread's <code>AppContext</code>. <code>AppContext</code> can be one
* or more(mostly for applets) per process. note) <code>AppContext</code>
* not a standard java API. it'll works on Sun JVM or it's variations.
*/
@SuppressWarnings("unchecked")
private static List<Window> getAllWindowsFromAppContext() throws Exception {
Object oRefs = AppContext.getAppContext().get(Window.class);
List<WeakReference<Window>> refs = (List<WeakReference<Window>>) oRefs;
List<Window> result = new ArrayList<Window>();
for (WeakReference<Window> wr : refs) {
Window w = wr.get();
result.add(w);
}
return result;
}

public static List<Window> getAllWindows() {
try {
return getAllWindowsFromStatic();
} catch (Exception e) {
try {
return getAllWindowsFromAppContext();
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
}

Change log

r9 by behumble on Apr 4, 2008   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r8 by behumble on Mar 23, 2008   Diff
getAllWindowsFromStatic()
getAllWindowsFromAppContext()
added
r7 by behumble on Mar 23, 2008   Diff
getAllWindows 추가
r6 by behumble on Mar 22, 2008   Diff
swing examples
All revisions of this file

File info

Size: 2817 bytes, 100 lines
Powered by Google Project Hosting