2017-05-01 5 views
0

は、私はこのスニペットを見つけたこの質問を検索しますtは私のために働く。のJavaFX非表示のTextArea-のVScrollBar

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.ScrollBar; 
import javafx.scene.control.TextArea; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 


public class Main extends Application { 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     BorderPane root = new BorderPane(); 

     TextArea textArea = new TextArea(); 

     root.setCenter(textArea); 

     Scene scene = new Scene(root, 400, 400); 
     scene.getStylesheets().add("Style.css"); 

     ScrollBar scrollBar = (ScrollBar) textArea.lookup(".scroll-bar:vertical"); 
     System.out.println(scrollBar); 
     scrollBar.setDisable(true); 

     primaryStage.setScene(scene); 

     primaryStage.show(); 
    } 
} 

CSS: 私は、最小限の例を作成

.text-area .scroll-bar:vertical:disabled { 
-fx-opacity: 0; 
} 

をしかし、私が得るすべてがnullpointer例外です。

Exception in Application start method 
null 
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method 
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException 
at Main.start(Main.java:33) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
... 1 more 

私は間違っていますか、それとも別の解決策がありますか?

+0

あなたが取得しているスタックトレースを表示できますか? – Flika205

+0

@ Flika205私はそれを追加しました – Abgehter

+0

この行にエラーが表示されているようです: 'scrollBar.setDisable(true);'。 scrollBarが初期化されていないため、[scrollBar API](http://docs.oracle.com/javafx/2/ui_controls/scrollbar.htm)をチェックしてください。 – Flika205

答えて

1

シーンがレンダリングされるまで、lookup()は機能しませんでした。シーケンスの変更は機能しました。

@Override 
public void start(Stage primaryStage) throws Exception { 

    BorderPane root = new BorderPane(); 

    TextArea textArea = new TextArea(); 

    root.setCenter(textArea); 

    Scene scene = new Scene(root, 400, 400); 
    scene.getStylesheets().add("Style.css"); 

    primaryStage.setScene(scene); 

    primaryStage.show(); 

    ScrollBar scrollBar = (ScrollBar) textArea.lookup(".scroll-bar:vertical"); 
    System.out.println(scrollBar); 
    scrollBar.setDisable(true); 
}