在外部单击节点上将BorderPane重新初始化为场景

时间:2014-04-01 17:55:45

标签: javafx-2

点击位于BorderPane中心区域的节点后,我正在尝试重新设计BorderPane的正确区域(使用新窗格)!

我确信这是一个很容易解决的问题,但是到目前为止,我还无法将其他资源的信息与我的具体问题放在一起。

另外,我希望答案仅涵盖javafx编码解决方案,因为到目前为止我还没有使用过fxml方法!

非常感谢您的帮助!

我的代码如下:

点击操作(在单独的课程中):

// StackPane action when clicked
dbShapeStackPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
     @Override public void handle(MouseEvent event) {

           //some other actions when clicked  

           //get logic for re-populate pane                        
           DataRAB_SectionReportPane reportListPane = new DataRAB_SectionReportPane();
           Pane reportList = reportListPane.addMainPane(false, reportDisplayName);   

           //activate main class
           BaseGUI mainTab = new BaseGUI();
           mainTab.addReportPane(reportList);               
    }
});

包含上述点击操作调用的单独方法的主类:

@Override
public void start (Stage primaryStage) {
        //attempt SQL PROMOTEX DB connection
        connection = new AddConnection();
        connection.makeConnection(); 

        innerPane = new BorderPane();   
        DataRAB_SectionReportPane dataRAB_reportPane = new DataRAB_SectionReportPane();
        reportList = new Pane();
        reportList = dataRAB_reportPane.addMainPane(true, "test Label!");
        reportList.setPrefSize(350, 200);

    DataRAB_SectionMenu dataRAB_Menu = new DataRAB_SectionMenu();
    pageMenu = dataRAB_Menu.addHBoxMenu(15,12,15,12);

    DataRAB_SectionValidateDataImportFiles validateData = new DataRAB_SectionValidateDataImportFiles();
    validateDataImport = validateData.buildValidationBorderPane();

    DataRAB_SectionDBsGrid dbGrid = new DataRAB_SectionDBsGrid();
    mainPageGridArea = dbGrid.buildDBGraphicalGrid();

    DataRAB_SectionInfoPane infoGrid = new DataRAB_SectionInfoPane();
    mainPageInfoGridArea = infoGrid.buildInfoGridPane();
    mainPageInfoGridArea.setPrefSize(200, 200);

    innerPane.setCenter(mainPageGridArea);
    innerPane.setTop(pageMenu);
    innerPane.setBottom(validateDataImport);
    innerPane.setRight(reportList);
    innerPane.setLeft(mainPageInfoGridArea);

    Region region = new Region();
    region.getStyleClass().add("stage-init");

    pageMenu.getChildren().add(region);
    primaryStage.initStyle(StageStyle.UNDECORATED);

    final BorderPane mainPane = new BorderPane();   
    final TabPane mainTabPane = new TabPane();

    final Tab data_RABTab = new Tab();
        data_RABTab.setText("Data Validation");
        mainTabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
        data_RABTab.setContent(innerPane);
        mainTabPane.getTabs().add(data_RABTab);

    final Main_ToolBar1 mainToolBar = new Main_ToolBar1();
        appMainToolBar = new ToolBar();
        appMainToolBar = mainToolBar.createMainToolBar(primaryStage, mainPageGridArea);
        appMainToolBar.setId("mainToolBar");
        mainPane.setCenter(mainTabPane);
        mainPane.setTop(appMainToolBar);

        scene = new Scene(mainPane, 1200, 700);
        scene.getStylesheets().add(BaseGUI.class.getResource("DataRAB_Styles.css").toExternalForm());     

        primaryStage.setScene(scene);
        primaryStage.show(); 
    }  

//re-add modified Pane to scene    

public void addReportPane(Pane pane){
    pane = new Pane();
    try {
          // innerPane = BorderPane original parent
          innerPane.getChildren().remove(innerPane.getRight());
          innerPane.setRight(pane);
    } catch(Exception ex) {
            Logger.getLogger(BaseGUI.class.getName()).log(Level.SEVERE,null,ex);
            System.out.println("Right part of Pane NOT removed!");
        }
}    

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

}

试图总结:

  • innerPane(= Parent BorderPane容器 - &gt;在main中定义 class:Base_GUI)

  • dbShapeGrid(= GridPane;放在innerPane的中心;包含 StackPane dbShapeStackPane作为孩子;所有定义,逻辑,儿童 dbShapeGrid存在于其自己的类中)

  • 我的目标是:当点击dbShapeStackPane时,应该根据逻辑在场景中重新初始化innerPane的右侧 方法:addMainPane; 我尝试了一种不同的方法如下(仍然没有成功):

dbShapeStackPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
     @Override public void handle(MouseEvent event) {                    
     //some other actions when clicked                               
     //get logic for re-populate pane ( from class: DataRAB_SectionReportPane)
     //logic is tested and works well if initialised first on scene                       
         DataRAB_SectionReportPane reportListPane = new DataRAB_SectionReportPane();
         Pane reportList = reportListPane.addMainPane(false, reportDisplayName);   
         //activate main class
         BaseGUI mainTab = new BaseGUI(); 
         final BorderPane bp = mainTab.getInnerPane();                        
         //try to re design the innerPane by using runnable
            Platform.runLater(new Runnable() {
                @Override
                public void run() {               
                   bp.setRight(reportList);
                }
            });               
         }
    });

...

dbShapeStackPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
     @Override public void handle(MouseEvent event) {                    
     //some other actions when clicked                               
     //get logic for re-populate pane ( from class: DataRAB_SectionReportPane)
     //logic is tested and works well if initialised first on scene                       
         DataRAB_SectionReportPane reportListPane = new DataRAB_SectionReportPane();
         Pane reportList = reportListPane.addMainPane(false, reportDisplayName);   
         //activate main class
         BaseGUI mainTab = new BaseGUI(); 
         final BorderPane bp = mainTab.getInnerPane();                        
         //try to re design the innerPane by using runnable
            Platform.runLater(new Runnable() {
                @Override
                public void run() {               
                   bp.setRight(reportList);
                }
            });               
         }
    });

1 个答案:

答案 0 :(得分:0)

方法addReportPane()始终将innerPane的右侧设置为空窗格,而不是尝试:

public void addReportPane(Pane pane) {
    try {
        innerPane.setRight(pane);
    } catch(Exception ex) {
        Logger.getLogger(BaseGUI.class.getName()).log(Level.SEVERE,null,ex);
        System.out.println("Right part of Pane NOT removed!");
    }
}

此外,DB数据检索应该在JavaFX主线程的其他线程(您提到的可运行)中完成。此外,JavaFX还为其提供了自己的功能API。在该站点搜索该主题(如“后台作业线程,数据库连接冻结”等),有很好的参考讨论和演示代码。

相关问题