0

I am trying to make a panel in javafx and i used to a border pane as a main scene. There are 4 windows (main1, main2, main3,main4) for center panel, and there is a navigation menu in left panel.

borderPane.setCenter(mainMenu1.getCenterMain1UI());
//borderPane.setCenter(mainMenu2.getCenterMain2UI());
//borderPane.setCenter(mainMenu3.getCenterMain3UI());
//borderPane.setCenter(mainMenu4.getCenterMain4UI());


public BorderPane getAppWindow(){

    if (borderPane == null){

        borderPane = new BorderPane();
        borderPane.setTop(topPanel.getTopPanelUI());
        borderPane.setBottom(bottomPanel.getBottomPanelUI());
        borderPane.setLeft(leftPanel.getLeftPanelUI());

        borderPane.setCenter(mainMenu.getCenterMainUI());
        borderPane.setAlignment(borderPane.getCenter(), Pos.TOP_LEFT);

    }

    return borderPane;
}

in the left panel controller

 public class LeftPanelController {

        public VBox leftPanelPane;

        public Button btnLeftPanelMainmenu;
        public Button btnLeftPanelDb;
        public Button btnLeftPanelOfficeInfo;
        public Button btnLeftPanelConfiguration;



        public void btnLeftPanelMainmenuOnClickAction(ActionEvent e){
            change border pane center to main
        }

        public void btnLeftPanelDbOnClickAction(ActionEvent e){
            change border pane center to DB
        }

        public void btnLeftPanelOfficeInfoOnClickAction(ActionEvent e){
            change border pane center to DB
        }

        public void btnLeftPanelConfigurationOnClickAction(ActionEvent e){
            change border pane center to configuration
        }

    }
stephan
  • 271
  • 1
  • 4
  • 24
  • You replace `center` 3 times in that code (or 4 times, if `center` wasn't `null` before calling the code). Try replacing the `center` once on interactions with the left panel. – fabian Oct 18 '16 at 12:55
  • It is really unclear for me what the actual question is. Call one of the code lines you posted in the click handler of the `Button`s of the left panel? – DVarga Oct 18 '16 at 12:56

2 Answers2

0

You'll need to set the on action event for each of your menu buttons so that it changes what is displayed in the center of the BorderPane.

Here's an example:

import java.util.LinkedHashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class BorderPaneExample extends Application {

    private Map<String, Node> menuOptions;
    private Node defaultCenterNode = new Label("Example node 1");

    @Override
    public void start(Stage primaryStage) throws Exception {

        menuOptions = new LinkedHashMap<>();  
        menuOptions.put("Main Menu", defaultCenterNode);
        menuOptions.put("History", new Label("Example node 2"));
        menuOptions.put("Office Info", new Label("Example node 3"));
        menuOptions.put("Configuration", new Label("Example node 4"));
        BorderPane root = new BorderPane();
        root.setCenter(defaultCenterNode);
        VBox menuLayout = new VBox();

        for (String key : menuOptions.keySet()) {
            Button button = new Button(key);
            button.setMaxWidth(Double.MAX_VALUE);
            button.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent event) {
                    root.setCenter(menuOptions.get(key));
                }
            });
            menuLayout.getChildren().add(button);
        }

        root.setLeft(menuLayout);

        Scene scene = new Scene(root, 800, 600);

        primaryStage.setScene(scene);
        primaryStage.setTitle("BorderPane Example");
        primaryStage.setResizable(false);
        primaryStage.sizeToScene();
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
d.j.brown
  • 1,822
  • 1
  • 12
  • 14
  • you're right but my mainStage, leftPanel and centerPanel are in the different directory. I pdated a screenshot about the hierarchy. – stephan Oct 18 '16 at 13:11
  • @enes Without knowing much about the classes, its hard to comment. However, assuming they extend `Node`, then you can just make new instances of those classes as per the `Label`'s in the example I provided. – d.j.brown Oct 18 '16 at 13:15
  • ok thanks so much, All components are the anchor pane in the center. there is a vbox in left. i updated a new image about the appWindow that calls all of the panels. – stephan Oct 18 '16 at 13:23
  • i changed my borderpane component from private to public static. So i resolved this cause. But i hope i wont get an error.. – stephan Oct 18 '16 at 13:33
  • Personally, I think the structure of your program needs a rethink. Add a method that allows you to update the center without needing to make the `BorderPane` public and certainly not static, e.g. `public void updateCenter(Node node)` – d.j.brown Oct 18 '16 at 13:37
0

I changed my button click methods in the left panel like that;

    public void btnLeftPanelMainmenuOnClickAction(ActionEvent e) {
        AppWindow.borderPane.setCenter(AppWindow.mainMenu.getCenterMainUI());
    }

    public void btnLeftPanelDbOnClickAction(ActionEvent e) {
        AppWindow.borderPane.setCenter(AppWindow.dbMenu.getCenterDbUI());
    }

    public void btnLeftPanelConfigurationOnClickAction(ActionEvent e) {
        AppWindow.borderPane.setCenter(AppWindow.configMenu.getCenterConfigurationUI());
    }
stephan
  • 271
  • 1
  • 4
  • 24