Solved

Always on top window (200 pts)

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 160
tomcat startup error 5 165
CSV file parsing thru Java 13 54
Java array 10 86
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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

737 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