這是我在這裏的第一篇文章,我也很新的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;
}
不過這個問題沒有成功!
您是否有意將新窗格分配爲,pane = new Pane();在方法addReportPane()?如果我要求你提出一個具體的問題,比如你期望從你的代碼中得到什麼,你會介意嗎?但是結果是什麼? –
嗨烏魯克!非常感謝您的回覆!這個想法是強制原始的innerPane重新初始化,在右邊設置新窗格! pane = new Pane()表示嘗試從click事件中調用addReportPane(),其中reportList也通過,並且應該是要重新初始化的新窗格!我得到了一個暗示,我必須使用runnables來實現我的目標,但是我可能錯了! – user3486131
根據我的代碼產生的結果,答案是:什麼都沒有!初始場景保持不變,通過addReportPane(reportList)傳遞給主類的新窗格(Pane reportList)不會替換舊窗格! – user3486131