Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Always on top window (200 pts)

Posted on 1998-09-01
5
Medium Priority
?
312 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 800 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

Independent Software Vendors: 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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

604 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