Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Keeping a java window (JDialog) 'always on top'

Posted on 1998-11-06
2
Medium Priority
?
913 Views
Last Modified: 2012-06-27
Is it possible for a java window to be forced to remain the topmost window?

For my purposes this app will always only run under Win95/98/NT so a native method approach is acceptable, and will work.  However, to do it that way I need a guaranteed way of getting the HWND for the window which I have not yet found.  Calling GetForegroundWindow won't do since there is no way to guarantee that the window is the foreground window when the call is made.
0
Comment
Question by:mrgentry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 200 total points
ID: 1227034
mrgentry, I have attached here a sample of code that does that. Note however, that on w95/NT, the window will be on op only if the aplication is active. Sothing that has to do with the way that the JVM on windows is implemented. See if you can use it.
Regarding your second question - the best way I found to find the window was to assign a unique name for each window (in the title), and the in the native method go throuh all the windows until you find the one with the expected name. However, I hope this code will do the trick for you. If you need more explenation, ask.



--------------------------------

   import java.awt.*;

   public class alwaysOnTop {

       /**
        * Constructor.
        */
       public alwaysOnTop () {
       }

       public static void main(String args[]) {
           myframe mf1 = new myframe();
           myframe mf2 = new myframe();
           myframe mf3 = new myframe();
           myframe mf4 = new myframe();
           ontopframe tf = new ontopframe();
           mf1.add(new Label("b1"));
           mf1.show();
           mf2.add(new Label("b2"));
           mf2.show();
           mf3.add(new Label("b3"));
           mf3.show();
           mf4.add(new Label("b4"));
           mf4.show();
           tf.add(new Label("Alway on top"));
           tf.show();
       }
   }

   class myframe extends Frame {
        public myframe() {
           setLayout( new FlowLayout());
        }

        public Dimension minumumSize() {
           return new Dimension( 200, 200 );
        }

        public Dimension preferredSize() {
           return new Dimension( 200, 200 );
        }

        public Dimension getPreferredSize() {
           return preferredSize();
        }
   }

   class ontopframe extends Frame implements Runnable {
        public final static int TO_TOP_TIME = 100; // Max time to top in milliseconds.
        Thread top_thread;
        boolean stop = false;

        public ontopframe() {
           setLayout( new FlowLayout());
           top_thread = new Thread(this);
           top_thread.start();
        }

        public Dimension minumumSize() {
           return new Dimension( 200, 200 );
        }

        public Dimension preferredSize() {
           return new Dimension( 200, 200 );
        }

        public Dimension getPreferredSize() {
           return preferredSize();
        }

        public void run() {
           while(!stop) {
               toFront();
               try{
                   top_thread.sleep(TO_TOP_TIME);
               } catch ( Exception e ){}
           }
        }
   }
0
 
LVL 1

Author Comment

by:mrgentry
ID: 1227035
I would really rather not have a thread just sitting there throwing the window to the front constantly and would much prefer to take the 2nd approach of searching for the widow.
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question