0

I am new in JavaFx and i have a question. I have 5 Anchor or SplitPanes and I need to switch between them by pressing buttons. How can I make it? And if you can please write a short example.

Here i place my code: It is my Main.class where I import all FXML files and controllers

public class MainApp extends Application{


private Stage primaryStage;
private BorderPane rootLayout;
private ArrayList<Costs> list = new ArrayList<>();

private SplitPane overView;
private AnchorPane history;
private AnchorPane center;


@Override
public void start(Stage primaryStage) {
    this.primaryStage=primaryStage;
    this.primaryStage.setTitle("Home Budget");

    initRootLayout();

    initOverView();
    rootLayout.setCenter(center);

    Scene scene = new Scene(rootLayout);
    primaryStage.setScene(scene);
    primaryStage.show();

}

private void initRootLayout() {
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("view/RootLayout.fxml"));
        rootLayout =(BorderPane)loader.load();

        RootLayoutController controller =loader.getController();
        }
    catch (IOException ex) {
        ex.printStackTrace();
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText("");
        alert.setContentText("Could not load FXML Loader");
        alert.showAndWait();
    }
}

public boolean showEditDialog(Costs costs){
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/ShowEditDialog.fxml"));
        AnchorPane pane =(AnchorPane) loader.load();

        Stage dialogStage = new Stage();
        dialogStage.setTitle("New expence");
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.initOwner(primaryStage);

        Scene scene = new Scene(pane);
        dialogStage.setScene(scene);
        ShowEditDialogController controller = loader.getController();
        controller.setDialogStage(dialogStage);
        dialogStage.showAndWait();
        return controller.isOkClicked();
        }
    catch (IOException ex) {
        ex.printStackTrace();
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText("");
        alert.setContentText("Could not load FXML Loader of Edit Dialog");
        alert.showAndWait();
        return false;
    }
}

public SplitPane initOverView(){
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/OverView.fxml"));
        overView = (SplitPane)loader.load();

        OverViewController controller = loader.getController();
        }
    catch (IOException ex) {
        ex.printStackTrace();
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText("");
        alert.setContentText("Could not load FXML Loader of Overview");
        alert.showAndWait();
    }
return overView;
}

public AnchorPane initHistory(){
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/History.fxml"));
        history = (AnchorPane)loader.load();
        HistoryController controller = loader.getController();
    }
    catch (IOException ex) {
        ex.printStackTrace();
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText("");
        alert.setContentText("Could not load FXML Loader of Overview");
        alert.showAndWait();
    }
return history;
}


public Stage getPrimaryStage() {
    return primaryStage;
}

public void setPrimaryStage(Stage primaryStage) {
    this.primaryStage = primaryStage;
}

public BorderPane getRootLayout() {
    return rootLayout;
}

public void setRootLayout(BorderPane rootLayout) {
    this.rootLayout = rootLayout;
}

public ArrayList<Costs> getList() {
    return list;
}

public void setList(ArrayList<Costs> list) {
    this.list = list;
}

public SplitPane getOverView() {
    return overView;
}

public void setOverView(SplitPane overView) {
    this.overView = overView;
}

public AnchorPane getHistory() {
    return history;
}

public void setHistory(AnchorPane history) {
    this.history = history;
}

public AnchorPane getCenter() {
    return center;
}

public void setCenter(AnchorPane center) {
    this.center = center;
}


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

And here is controller for RootLayout from which I want to change Panes

public class RootLayoutController implements Initializable{

MainApp mainApp = new MainApp();

@FXML
private BorderPane root;
@FXML
private Button overView;
@FXML
private Button History;

@FXML
private void handleNew(){

    Costs costs = new Costs();
    boolean okClicked = mainApp.showEditDialog(costs);
}

@FXML
private void handleHistory(){

}

@FXML
private void handleOverView(){

}

@Override
public void initialize(URL location, ResourceBundle resources) {

} 
}

And last one my RootLayout FXML File

        <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane prefHeight="375.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="apg.budget.view.RootLayoutController">
   <left>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="375.0" prefWidth="150.0" BorderPane.alignment="CENTER">
         <children>
            <Group />
            <Button alignment="BOTTOM_LEFT" onAction="#handleOverView" prefHeight="35.0" prefWidth="130.0" text="Overview" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutY="35.0" mnemonicParsing="false" onAction="#handleHistory" prefHeight="35.0" prefWidth="130.0" text="History" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutX="1.0" layoutY="70.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="130.0" text="Budget" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutY="105.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="130.0" text="Distribution" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutX="1.0" layoutY="140.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="130.0" text="Statistics" textAlignment="CENTER" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutX="1.0" layoutY="340.0" mnemonicParsing="false" onAction="#handleNew" prefHeight="35.0" prefWidth="130.0" text="New" textAlignment="CENTER" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
         </children>
      </AnchorPane>
   </left>
</BorderPane>

Thank you for help!

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
  • Hi! You should add your AnchorPane, Pane, Whatever, into a StackPane, and then use node.toFront() to bring the wanted one on top of the whole stack – Psychokiller1888 Sep 23 '15 at 08:53
  • Does your button click loads a new AnchorPane at the same location in BorderPane where the old AnchorPane was placed? – ItachiUchiha Sep 23 '15 at 09:46
  • When I try load AnchorPane by button Click, appears NullPointerException . I try to getChildren() of BorderLayout, clear them and put new AnchorPane to Center, but it doesn`t work. So you tell me to put StackPane into the center of BorderLayout and then chage panes? – Apostu Pasha Sep 23 '15 at 10:20
  • Why you create a new MainApp instance in your RootLayoutController? You should give it a reference to your exisiting MainApp! – aw-think Sep 23 '15 at 10:34
  • i have changed AnchorPane to the StackPane, but when i add Nodes to the StackPane appeared NullPointerException((( Here is fragment of code of my changer Main class. – Apostu Pasha Sep 23 '15 at 10:57
  • @ApostuPasha You get the NullPointerException because you doing things on the wrong Object! You cannot create two Application Instances in one Application. You need a method in your RootLayoutController like `setMainApp(MainApp mainApp)` and after you called `RootLayoutController controller =loader.getController();` you can easily set the the reference by `controller.setMainApp(this)` – aw-think Sep 23 '15 at 11:21
  • @NwDx. Thank you very much. With your help it works. – Apostu Pasha Sep 23 '15 at 12:04

0 Answers0