2014-04-01 76 views
1

这是我在这里的第一篇文章,我也很新的JavaFX和Java的一般编码,因此,请与我裸露!重新初始化BorderPane到现场对外界点击节点

我试图点击位于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(=父BorderPane容器 - >在主 类中定义: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); 
       } 
      });    
     } 
    }); 

...

//replaced old method addReportPane (in main class: BaseGUI) with the following: 
    public BorderPane getInnerPane(){ 
     return innerPane; 
    } 

不过这个问题没有成功!

+0

您是否有意将新窗格分配为,pane = new Pane();在方法addReportPane()?如果我要求你提出一个具体的问题,比如你期望从你的代码中得到什么,你会介意吗?但是结果是什么? –

+0

嗨乌鲁克!非常感谢您的回复!这个想法是强制原始的innerPane重新初始化,在右边设置新窗格! pane = new Pane()表示尝试从click事件中调用addReportPane(),其中reportList也通过,并且应该是要重新初始化的新窗格!我得到了一个暗示,我必须使用runnables来实现我的目标,但是我可能错了! – user3486131

+0

根据我的代码产生的结果,答案是:什么都没有!初始场景保持不变,通过addReportPane(reportList)传递给主类的新窗格(Pane reportList)不会替换旧窗格! – user3486131

回答

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。搜索该网站的主题(如“后台作业线程,数据库连接冻结”等),有很好的参考讨论和演示代码。

+0

嗨!点好了!我之前的“答案”在原始问题中作为编辑被移动!谢谢! – user3486131