Question of the Week No. 62
Researched by Rama Roberts
Question of the Week presents answers to key questions posed by the
developer community. The intent is to pass this important, but not
always easy-to-find, information on to JavaTM
Developer ConnectionSM (JDC) members. The
questions are selected from the JDC newsgroups generally because: they are
frequently asked, they are significant or timely, or their answers are not
easily accessible.
Note: If you have a question to which you need an answer, try the JDC newsgroups. You can read through
the existing newsgroups, or with your
free JDC membership, you can post
new messages or threads.
Topic:
How can I bring up an iconified JFrame?
Question: Will someone tell me how to bring up a JFrame from the task bar without actually clicking on it?
In my application, when a user tries to open up a window, my application
checks for a window open already. If one is open, it will bring it up to the front.
Answer:
If you are using JDKTM 1.2.x you can use the Frame class's setState() method passing it 'Frame.NORMAL' to unminimize a frame. See attached sample working code that unminimizes a JFrame two seconds after it's been minimized.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Unminimize {
public static void main(String args[]) {
new UnminimizeFrame();
}
}
class UnminimizeFrame extends JFrame {
UnminimizeFrame() {
super();
// Components should be added to
//the container's content pane
Container cp = getContentPane();
/* Add the window listener */
addWindowListener(new WindowAdapter() {
public void windowClosing(
WindowEvent evt) {
dispose();
System.exit(0);
}
public void windowIconified(
WindowEvent evt) {
try {
Thread.sleep(2000);
}
catch(InterruptedException ie){}
setState(Frame.NORMAL);
}
});
/* Size the frame */
setSize(200,200);
/* Center the frame */
Dimension screenDim =
Toolkit.getDefaultToolkit(
).getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width -
frameDim.width) / 2,(screenDim.height -
frameDim.height) / 2);
/* Show the frame */
setVisible(true);
}
}
Many thanks to JDC member paternostro for contributing to
this answer.
Submit your comments or answers to this question here!
See what other members have
said.