Solved

Always on top window (200 pts)

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

943 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

6 Experts available now in Live!

Get 1:1 Help Now