|
Swing JFrame
Part:
1
2
3
(Continued from previous part...)
Interrupting Frame Threads
Problem: I want to know how many threads are created for the frame window
and can those threads been interrupted.
Solution: This is easy. Just get the list of all threads of the current
thread group, and interrupt them one by one. Here is a sample program to show
you how to do this:
/**
* FrameThreads.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.awt.*;
import javax.swing.*;
public class FrameThreads {
public static void main(String[] a) {
JFrame f = new JFrame("Frame 1");
f.setBounds(0,0,100,100);
f.setVisible(true);
f = new JFrame("Frame 2");
f.setBounds(50,50,100,100);
f.setVisible(true);
f = new JFrame("Frame 3");
f.setBounds(100,100,100,100);
f.setVisible(true);
Thread[] l = new Thread[100];
int n = Thread.enumerate(l);
Thread g = null;
for (int i=0; i<n; i++) {
System.out.println("Active thread = "+l[i].getName());
}
Thread t = Thread.currentThread();
for (int i=0; i<n; i++) {
try {
Thread.sleep(1000*30);
} catch (Exception e) {
System.out.println("Interrupted.");
}
if (t != l[i]) {
System.out.println("Interrupting thread = "
+l[i].getName());
l[i].interrupt();
}
}
}
}
If you run this program, you will see 3 frame windows showing up on the screen.
After about every 30 seconds, my program will send an interrupt signal to each
thread. When the interrupt signal reaches "Java2D Disposer" thread, all the
windows will be closed. The following list shows you the output of my program:
Active thread = main
Active thread = AWT-Windows
Active thread = AWT-Shutdown
Active thread = AWT-EventQueue-0
Active thread = Java2D Disposer
Interrupting thread = AWT-Windows
Interrupting thread = AWT-Shutdown
Interrupting thread = AWT-EventQueue-0
Interrupting thread = Java2D Disposer
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:111)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:127)
at sun.java2d.Disposer.run(Disposer.java:100)
at java.lang.Thread.run(Thread.java:534)
AWT blocker activation interrupted:
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:429)
at sun.awt.AWTAutoShutdown.activateBlockerThread(AWTAutoShut...
at sun.awt.AWTAutoShutdown.setToolkitBusy(AWTAutoShutdown.ja...
at sun.awt.AWTAutoShutdown.notifyToolkitThreadBusy(AWTAutoSh...
at sun.awt.windows.WToolkit.eventLoop(Native Method)
at sun.awt.windows.WToolkit.run(WToolkit.java:262)
at java.lang.Thread.run(Thread.java:534)
Note 1: Swing system uses 4 threads: AWT-Windows, AWT-Shutdown, AWT-EventQueue-0,
and Java2D Disposer.
Note 2: When thread "Java2D Disposer" is interrupted, the entire application terminates.
Displaying Chinese Characters in Frame Title
Problem: I want to display Chinese characters in the title of a frame
window.
Solution: You can only do this if you are using JDK 1.4.2 and have a
Unicode font installed to support Chinese characters. In my sample code listed
below, I am using font SimSun, which was installed as part of Windows
multi-language support.
/**
* JFrameChinese.java
* Copyright (c) 2004 by Dr. Herong Yang
*/
import java.awt.*;
import javax.swing.*;
public class JFrameChinese {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setFont(new Font("SimSun",Font.PLAIN, 12));
f.setTitle("Hello world! - \u7535\u8111\u4F60\u597D\uFF01");
f.setBounds(100,50,500,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Note 1: With JDK 1.4.1 or lower, the characters are displayed as "?".
I don't any way to fix it. If you have any suggestions, please share them
with me.
Note 2: To check if you have SimSun font installed or not, you can look
for \winnt\fonts\simsun.ttc file in your system directory tree.
(Continued on next part...)
Part:
1
2
3
|