Solved

Always on top window (200 pts)

Posted on 1998-09-01
5
263 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
  • 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now