We are having a problem getting our Swing application to display in front of other
running
applications the FIRST time that it attempts to display a JFrame. To provide some
background, we have a Java application which is always running and which provides a
TCP/IP
server. Client applications written in other languages can connect to this server and
request it to execute Java classes. Some of these requests involve launching a JFrame
window. On NT only, the very FIRST time that such a request is made, the JFrame is
displayed behind the requesting application. The second and later times such a
request is
received, the Java application displays correctly on top. Our application always runs
correctly on Unix (Solaris and Linux) and OS/2.
In order to illustrate the problem, I developed a small test application (Java source
code
attached). To reproduce the problem, compile the source and type the following:
java DelayedDisplay
in a command window. Then bring up some other application (e.g. Netscape or IE) and
make
sure that it is close to the upper left hand corner. After about 30 seconds, our
JFrame
should pop up. Its position is (0, 0), but it will be hidden by the other application.
Now click on the hide button and give the other GUI application the focus again. In
another 30 seconds the JFrame will pop up again, but this time it will display on top
of
the other GUI application which is what we want.
You can set the delay to a shorter time with the following:
java DelayedDisplay 10
where "10" would be the wait time in seconds.
My source code attempts to incorporate all of the suggestions previously made on the
Swing
list, but none seem to solve the problem. We are using the Sun JDK 1.3.1 for Windows.
My
test machine has Windows NT FixPak 3.
To kill the application, just press the window close icon.
Cynthia Jeness
Foudili Kacem wrote:
> Hi Cynthia,
> Hi, I had a similar problem time ago, and I modified
> my code as following to get a better result...
>
> if(frame.getState()==Frame.ICONIFIED)
> frame.setState(Frame.NORMAL);
> frame.setVisible(true);
> frame.requestFocus();
>
> Hope it helps.
> K.
>
> ------------------------------------
> At 12:59 22/09/2001 -0400, you wrote:
>
> Christian,
>
> Here is the code that I use. I added the "toFront()" and changed "show()"
> to
> "setVisible(true)", but this has no effect. Again, I don't have any
> problem if
> I simply start a Swing applicaton on NT. The problem arises when we have an
> already running non-GUI TCP/IP Java server application which is request to
> start
> a Swing application. The problem occurs whether or not the the MSDOS window
> containing the TCP/IP Java server is maximized and has the focus or not.
>
> Cynthia Jeness
>
>
>----------------------------------------------------------------------------------------
>
> /**
> * This method sizes the frame and shows it. The window is
> * sized at a maximum of MAX_WINDOW_WIDTH X MAX_WINDOW_HEIGHT and
> * centered on the screen.
> */
> public void showFrame() {
>
> // If any error during initialization, then return.
> if (initError)
> return;
>
> // Size the frame and display it in the middle of the screen.
> Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
> int width = Math.min(Constants.MAX_WINDOW_WIDTH, screen.width);
> int height = Math.min(Constants.MAX_WINDOW_HEIGHT, screen.height);
> int left = (screen.width - width)/2;
> int top = (screen.height - height)/2;
> setBounds(left, top, width, height);
>
> // Trying to force visibility on Windows.
> toFront();
> setVisible(true);
> //show();
> windowOpen = true;
> }
>
> Christian Pesch wrote:
>
> >Cynthia Jeness schrieb:
> >
> > > We have a legacy GUI application which communicates with a Java TCP/IP
> > > server application running on the same computer. Depending on the type
> > > of message received, we load a JFrame which contains a JTabbedPane. The
> > > application is launched correctly. However, at least under NT, it is
> > > minimized and, thus, you must select it from the Taskbar to actually see
> > > it. The application is launched correctly under OS/2. In the case of
> > > NT, we are using the latest JDK 1.3.1 from Sun. I have tried explicitly
> > > setting the state of the frame to NORMAL. But this has no effect.
> >
> >I use the following code sequence to open frames and bring them to front
> >with JDK 1.3.1 Windows NT and 2000:
> >
> >if(System.getProperty("os.name").startsWith("Windows")) {
> > Gui.getLog().write(Log.LEVEL_INFO, "Enabled Windows window to front
> >patch");
> >
> > enableWindowsPatch = true;
> >}
> >
> >Then opening does this
> >
> > // workaround for windows to bring frame to front
> > if(enableWindowsPatch)
> > frame.toFront();
> >
> > frame.setVisible(true);
> >
> > > Does
> > > anyone have any suggestions about how to get the application launched in
> > > a normal size. If I launch it from the command line on NT rather than
> > > from an already running Java application, then the window is displayed
> > > correctly on NT.
> >
> >Please extract your code sequence to show a frame. May that shows up
> >an error.
> >
> >--
> >Christian Pesch - Software Engineer
> >CoreMedia AG - http://www.coremedia.com - 0700-COREMEDIA
> >Visit us on the Systems, M�nchen and Orbit, Basel
>
> _______________________________________________
> Swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/swing
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
> _______________________________________________
> Swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/swing
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* The purpose of this class is to illustrate a problem in displaying
* Swing applications on NT. In particular, the Swing application will
* not move to the top of the "Z-order" the first time its frame is
* displayed.
*/
public class DelayedDisplay {
private int delay;
public DelayedDisplay(int delay) {
this.delay = delay;
}
public void showFrame() {
final JFrame frame = new JFrame("Delayed Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Hide");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.hide();
frame.dispose();
waitAndShow();
}
});
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.setBounds(0, 0, 200, 200);
frame.toFront();
frame.setVisible(true);
frame.requestFocus();
}
public void waitAndShow() {
try {
Thread.sleep(delay * 1000);
}
catch(Exception e) {}
showFrame();
}
/**
* This main method parses the first argument as the time display
* and then uses this to construct the "DelayedDisplay" object.
*
* @param args[0] Time delay in seconds (default 30).
*/
public static void main(String [] args) {
int delay = 30;
if (args.length > 0)
delay = Integer.parseInt(args[0]);
DelayedDisplay dd = new DelayedDisplay(delay);
dd.waitAndShow();
}
}