2010-01-09 16 views
6

(SOLUCIONADO: un WindowStateListener y una llamada diferida a toBack siempre que la ventana tiene el foco)Cómo hacer una ventana Unfocusable en Java

Hola a todos!

He estado tratando de averiguar cómo hacer un java.awt.Window (cualquier subclase servirá) para que no se pueda llevar al frente. Estoy trabajando en un programa "tipo Samurize" de Java que aparece debajo de todas las ventanas de la aplicación y muestra Widgets en la pantalla. Al igual que "Always on top windows with Java", espero algo simple, con suerte una sola llamada a un método, si es posible, pero he verificado los documentos de la API y no he tenido suerte.

Edit: Lo siento, quise decir "siempre en el fondo" en lugar de simplemente "fuera de foco".

Aquí hay un caso de prueba básico. Al hacer clic en la ventana, no debe estar por sobre cualquier otro momento en la pantalla:

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); 
    } 
} 

Respuesta

8

Desea utilizar setFocusableWindowState(false)

(fwiw, esto fue en el documento API unidos por la respuesta superior de la publicación a la que se refiere)

editar: ¿qué hay de agregar un detector al cambio de estado de la ventana que ejecuta toBack()?

editar: también puede considerar anular el método toFront para evitar que algo pueda tirar de la ventana hacia el frente.

+0

Eso debería funcionar, pero al menos en mi Mac OS X 10.6 Sistema de la ventana se puede seguir enfocado. –

+0

Editado, lo siento. Ver publicación. –

+0

Aha! Me lo imaginé. Solo necesita diferir la invocación de 'toBack()' con 'NSTimer' cuando se hace el manejo de WindowStateListener. –

1

setFocusableWindowState

pública setFocusableWindowState vacío (focusableWindowState booleano)

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

Cuestiones relacionadas