2017-04-27 78 views
1

我想在某些用户操作后显示NotificationPane。我的应用程序有多个场景,NotificationPane应显示在当前活动的场景中。NotificationPane不会出现在场景

整件事情与通知一起工作,它在我需要它时弹出。 但我不知道如何使这项工作的NotificationPane。

步骤我迄今所取得:

  • 我tryed直接把NotificationPane我的现场,并拨打 show() - 它的工作原理。
  • 如今的想法是通过调用 stage.getScene().getRoot()获得当前窗格,它换到NotificationPane,然后调用 show() - 它不工作,我不知道为什么。
  • ((BorderPane) pane).setCenter(new Label("TEST"));此行与文本标签更换按钮,所以stage.getScene().getRoot()将返回正确的对象

我做了一个简单的程序来测试性能。一个按钮可以调用NotificationPane。 有什么建议吗?

这里是我的测试程序:

package application; 

import org.controlsfx.control.NotificationPane; 

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class Main extends Application { 

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

    @Override 
    public void start(Stage primaryStage) { 
     Button notificationPaneButton = new Button("NotificationPane"); 
     notificationPaneButton.setOnAction(e -> showNotificationPane(primaryStage, "Notification text")); 

     VBox vbox = new VBox(5); 
     vbox.setAlignment(Pos.CENTER); 
     vbox.getChildren().addAll(notificationPaneButton); 

     BorderPane borderPane = new BorderPane(); 
     borderPane.setCenter(vbox); 

     primaryStage.setTitle("Notifications test"); 
     primaryStage.setScene(new Scene(borderPane, 300, 200)); 
     primaryStage.show(); 
    } 

    public void showNotificationPane(Stage stage, String message) { 
     Parent pane = stage.getScene().getRoot(); 
//  ((BorderPane) pane).setCenter(new Label("TEST")); 
     NotificationPane notificationPane = new NotificationPane(pane); 
     notificationPane.setText(message); 
     if (notificationPane.showingProperty().get()) { 
      notificationPane.hide(); 
      System.err.println("hide"); 
     } else { 
      notificationPane.show(); 
      System.err.println("show"); 
     } 

    } 
} 

回答

1

好吧,我现在看到的问题。包装当前窗格是不够的,我还必须将NotificationPane添加到场景中。对?

反正我目前的解决方案是以下几点:

  • 获得当前场景
  • 获得当前窗格
  • 包装窗格
  • 用新

为了避免包装替换当前场景NotificationPane多次检查当前窗格是否已经为NotificationPane,然后c全部为show()

public void showNotificationPane(Stage stage) { 
    Scene scene = stage.getScene(); 
    Parent pane = scene.getRoot(); 
    if (!(pane instanceof NotificationPane)){ 
     NotificationPane notificationPane = new NotificationPane(pane); 
     scene = new Scene(notificationPane, scene.getWidth(), scene.getHeight()); 
     stage.setScene(scene); 
     notificationPane.show(); 
    } else { 
     ((NotificationPane)pane).show(); 
    } 
}