views:

204

answers:

2

(SOLVED: a WindowStateListener and a deferred call to toBack whenever the window is focused)

Hello all!

I've been trying to figure out how to make a java.awt.Window (any subclass will do) so that it cannot be brought to the front. I'm working on a Java "Samurize-like" program that appears below all the application windows and displays Widgets on the screen. Just like "Always on top windows with Java", I'm hoping for something simple, hopefully just a single method call, if possible, but I've checked through the API docs and I've had no luck.

Edit: Sorry, I meant "always on bottom" rather than simply "unfocusable".

Here's a basic test case. When clicking on the Window, it should not come above any others currently on the screen:

import java.awt.*;
import javax.swing.*;

public class Main extends JFrame {
    public Main() {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        setFocusable(false);
        setFocusableWindowState(false);
        setBounds(new Rectangle(dim));

        toBack();
    }

    public static void main(String[] args) {
        new Main().setVisible(true);
    }
}
+2  A: 

You want to use setFocusableWindowState(false)

(fwiw, this was in the API document linked by the top answer of the post you referred to)

edit: what about adding a listener to window state change that executes toBack()?

edit: you might also consider overriding the toFront method to prevent anything from pulling the window to the front.

Mark E
That should work, but at least on my Mac OS X System 10.6 the window can still be focused.
Fat Lotus
Edited, sorry. See post.
Fat Lotus
Aha! I figured it out. You just need to defer calling of `toBack()` with an `NSTimer` when doing the WindowStateListener handling.
Fat Lotus
+1  A: 

setFocusableWindowState

public void setFocusableWindowState(boolean focusableWindowState)

Sets whether this Window can become the focused Window if it meets the other requirements outlined in isFocusableWindow. If this Window's focusable Window state is set to false, then isFocusableWindow will return false. If this Window's focusable Window state is set to true, then isFocusableWindow may return true or false depending upon the other requirements which must be met in order for a Window to be focusable.

Setting a Window's focusability state to false is the standard mechanism for an application to identify to the AWT a Window which will be used as a floating palette or toolbar, and thus should be a non-focusable Window. Setting the focusability state on a visible Window can have a delayed effect on some platforms — the actual change may happen only when the Window becomes hidden and then visible again. To ensure consistent behavior across platforms, set the Window's focusable state when the WIndow is invisible and then show it.

Parameters:
    focusableWindowState - whether this Window can be the focused Window
Since:
    1.4
See Also:
    isFocusableWindow(), getFocusableWindowState(), isShowing(), Component.setFocusable(boolean)

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html#setFocusableWindowState%28boolean%29

zipcodeman