Solved

Always on top window (200 pts)

Posted on 1998-09-01
5
299 Views
Last Modified: 2010-03-30
I'd like to an always on top Frame window in an application.
I tried to use toFront when the window loses the focus.
But it doesn't work. Idem in the windowDeactivated function.
How can I do?
Could you give me some code?
Thanx
0
Comment
Question by:vbonduel
[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
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 200 total points
ID: 1223107
vbonduel, what you should do is create an image that is attached to a thread that constantly sends it to the top. Here is an example:




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 6

Expert Comment

by:gadio
ID: 1223108
note that the TO_TOP_TIME variable determines what is the rate of sending the frame to the top. I set it to 1/10 of a second and it looks good. You can test with other values.
Hope that helps, G.
0
 

Author Comment

by:vbonduel
ID: 1223109
Well, it works, but only with Java windows (only with mf1, mf2, mf3 and mf4). With other applications (other windows)it doesn't work. I don't understand why. Have you an answer?
Thanx again.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1223110
The Window.toFront() function is implemented by the virtual machine, and on the windows implementation they chose to do that only if the application is in focus. Its an implementation dependent thing. On Unix machines (I checked it on Sun and on Silicon Graphics), the window is always on top.
0
 

Author Comment

by:vbonduel
ID: 1223111
Thank you.
I think the appropriate grade for you is A =)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

729 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