Relevant Files: (apologies on any formatting, made many attempts to make it work)
If the files are not sufficient, the repository is here: https://github.com/TheeNinja/StockLookupTool
stock_lookup_tool_main.fxml
<BorderPane fx:id="borderPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.theeninja.stocklookuptool.gui.StockLookupToolApplicationController">
<top>
...
</top>
<left />
<center />
</BorderPane>
stock_information_center.fxml
<GridPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="me.theeninja.stocklookuptool.gui.selection.StockSearchSelectionController"
prefHeight="400.0" prefWidth="600.0"
fx:id="stockInformationCenter">
</GridPane>
favorite_stocks_sidebar.fxml
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.theeninja.stocklookuptool.gui.selection.StockSearchSelectionController" fx:id="verticalStockList">
...
</VBox>
StockLookupTool.java (Class with the start method)
public class StockLookupTool extends Application {
....
@Override
public void start(Stage stage) throws Exception {
...
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/stock_lookup_tool_main.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, 250, 300);
stage.setTitle(APPLICATION_TITLE);
stage.setScene(scene);
stage.setFullScreen(true);
stage.show();
}
....
}
StockLookupToolApplicationController (controller for stock_lookup_tool_main.fxml)
public class StockLookupToolApplicationController {
...
@FXML public Label stockSearchLabel;
@FXML public Label newsLabel;
@FXML public Label settings;
@FXML public BorderPane borderPane;
@FXML public HBox topApplicationNavigation;
@FXML
public void handleStockSearchSelection() {
logger.log(Level.INFO, "Setting GUI View to Stock Search");
setView(StockSearchSelectionController.getInstance());
}
private void setView(Selection selection) {
borderPane.setLeft(selection.getLeft());
borderPane.setCenter(selection.getCenter());
borderPane.setRight(selection.getRight());
borderPane.setBottom(selection.getBottom());
}
}
StockSearchSelectionController.java (controller for both stock_information_center.fxml and favorite_stocks_sidebar.fxml)
public class StockSearchSelectionController implements Selection {
@FXML public Label favoriteStocksLabel;
@FXML public TextField addFavoriteStockInput;
@FXML public VBox verticalStockList;
@FXML public HBox addFavoriteStockInputContainer;
@FXML public GridPane stockInformationCenter;
@FXML
public void handleFavoriteStockInput(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
...
// makes visual changes to both stockInformationCenter AND verticalStockList, hence I need access to both fxml files (which is why this controls both).
}
}
@Override
public Node getLeft() {
return verticalStockList;
}
@Override
public Node getCenter() {
return stockInformationCenter;
}
}
More information:
The interface Selection
has the methods getLeft()
and getCenter()
(among other irrelevant ones). Both of these returns Node
. In stock_lookup_tool_main.fxml
, in the top
portion of the BorderPane
I have a button (cut out in the provided snippet) that, when pressed, calls handleStockSearchSelection()
(this method call indeed happens, I have verified it with a logger).
Now there is a controller, that controls both stock_information_center.fxml
and favorite_stocks_sidebar.fxml
. This controller implements Selection
, and in turn, implements getLeft()
and getCenter()
. getLeft()
returns the VBox variable
that correlates to favorite_stocks_sidebar.fxml
, while getCenter()
returns the GridPane variable that correlates to stock_information_center.fxml
. When handleStockSearchSelection()
is called, the left part of the borderPane object is set to the VBox, and the center is set to the GridPane. All these method calls/actions have been verified to occur. However, there is no visual change to the scene.
My question is: Why are these changes to borderPane not implemented visually in the scene? I have established a link between the scene <-> stock_lookup_tool_main.fxml
through setting the loader's location. I have also established a link between stock_lookup_tool_main.fxml
<-> its controller, hence they should share changes. I have modified the border pane in stock_lookup_tool_main.fxml
in the controller by calling setLeft() and setCenter() on the variable (same name as the ID, borderPane). Yet, the scene visually does not change.