2015-02-17 75 views
1

我有一個應用程序,有很多階段,做各種不同的事情。我想知道是否可以更改整個應用程序的Cursor,而不必爲所有場景進行更改。JavaFX更改所有階段的光標

例如,如果用戶做了長時間運行的任務,我想光標變爲等待光標用於所有場景。當這個任務完成後,我希望光標變回正常光標。

我明白,要改變光標爲特定的場景,你可以做

scene.setCursor(Cursor.WAIT); 

我寧可不要通過所有在我的應用程序的各個階段的迭代,並改變光標的每一個。

我想知道,如果你可以在應用程序級別更改光標,而不是現場級。我沒有發現任何網絡上的任何暗示你可以。

回答

1

有在應用層面做到這一點(我知道的),沒有直接的方法。但是,遊標是一個屬性,因此您可以將所有場景的遊標綁定到單個值。

因此,像:

public class MyApp extends Application { 

    private final ObjectProperty<Cursor> cursor = new SimpleObjectProperty<>(Cursor.DEFAULT); 

    @Override 
    public void start(Stage primaryStage) { 
     Parent root = ... ; 
     // ... 

     someButton.setOnAction(event -> { 
      Parent stageRoot = ... ; 
      Stage anotherStage = new Stage(); 
      anotherStage.setScene(createScene(stageRoot, ..., ...)); 
      anotherStage.show(); 
     }); 

     primaryStage.setScene(createScene(root, width, height)); 
     primaryStage.show(); 

    } 

    private static Scene createScene(Parent root, double width, double height) { 
     Scene scene = new Scene(root, width, height); 
     scene.cursorProperty().bind(cursor); 
     return scene ; 
    } 
} 

現在,任何時候你做

cursor.set(Cursor.WAIT); 

通過createScene(...)方法將改變其光標創建任何場景。

顯然光標屬性和實用方法沒有在應用程序的子類來定義;你可以把它們放在你的應用程序結構方便的地方。