Im looking at this problem from in a new way:
I have a simple FXML app that has a mainapp, a controller and a view. The view displays a gridpane consisting of three rows where each row contains a single label element. So the gridpane looks like this to the user:
row 1
row 2
row 3
There is also a button called Delete Selected Row that is clicked after the user selects a row. When the button is clicked, the element is temporarily removed, or hidden, from the gridpane. So if row 2 is selected and the button is clicked the user will see
row 1
row 3
The above view is created with Scene Builder that creates an XML file called AppOverview.fxml
.
There is a MainApp
that paints the scene that has the following code:
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
//Constructor
public MainApp() {}
@Override
public void start(Stage primaryStage)
{
this.primaryStage = primaryStage;
primaryStage.initStyle(StageStyle.UNDECORATED);
initRootLayout();
showAppOverview();
}
// Initializes the root layout.
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
System.out.println("Error in MainApp:initRootLayout()");
System.exit(0);
}
}
// Shows the app overview inside the root layout.
public void showAppOverview() {
try {
// Load overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/AppOverview.fxml"));
AnchorPane appOverview = (AnchorPane) loader.load();
// Set overview into the center of root layout.
rootLayout.setCenter(appOverview);
// Give the controller access to the main app.
AppController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
System.exit(0);
}
}
// Returns the main stage.
public Stage getPrimaryStage(){
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
The controller in my app.view package contains listeners on the handleDeleteBtn
button (and a couple other buttons) and handleSelectedGridPaneRow()
to manage row selection in the gridpane:
public class AppController {
//Reference to MainApp
private MainApp mainApp;
@FXML
private void handleRestoreBtn(){
System.out.println("You are restoring the hidden row");
}
@FXML
private void handleDeleteBtn(){
System.out.println("You are hiding the selected row");
}
@FXML
private void handleExitBtn(){
System.exit(0);
}
@FXML
private void handleSelectedGridPaneRow(){
System.out.println("You have selected a row to hide");
}
public AppController(){}
// Initialize the <code>AppController</code> instance.
@FXML
private void initialize(){}
public void setMainApp(MainApp mainApp)
{
this.mainApp = mainApp;
}
}
My specific questions are:
1) Is this kind of spec achievable, ie, being able to dynamically manage a gridpane as described?
2) If yes, where and how would I access the gridpane? Presumably somewhere in the method 'handleDeleteBtn()' in AppController
. What do I need to do to reference the gridpane in the Controller and then execute statements such as gridPane.getChildren().remove(0,2);
?