0

In my project i am adding a new fxml file inside a scrollpane using menubox at start menubox is setvisible(false); now after calling a fxml inside scrollpane that contains a button now inside the controller i made a function which makes menubox setvisible(true)

My complete codes:

ContentNavigator.java

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;

/**

 */
public class ContentNavigator {

    //fxml files
    public static String CONTENT1 = "masterLogin.fxml";
    public static String CONTENT2 = "FeesAddItem.fxml";
     public static String CONTENT3 = "home.fxml";


    private static DashboardController mainController;

    public static void setController(DashboardController controller){
        ContentNavigator.mainController = controller;
    }

    public static void loadContent(String fxmlFile){
        try {
            mainController.setContent(
                    (AnchorPane) FXMLLoader.load(ContentNavigator.class.getResource(fxmlFile))
            );
        } catch (IOException ex) {
            Logger.getLogger(ContentNavigator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

.

  public void start(Stage stage) throws Exception {
           FXMLLoader loader = new FXMLLoader();
        AnchorPane mainWindow = (AnchorPane) loader.load(getClass().getResourceAsStream("masterboard.fxml"));
        Scene scene = new Scene(mainWindow);

       scene.getStylesheets().add("css/main.css");

        stage.setTitle("JavaFX");

        stage.setScene(scene);
         stage.initStyle(StageStyle.UNDECORATED);

        stage.show();

        DashboardController controller = loader.getController();
        ContentNavigator.setController(controller);
        ContentNavigator.loadContent(ContentNavigator.CONTENT1);

    }

masterboard controller

@Override
    public void initialize(URL url, ResourceBundle rb) {
   menubox.setVisible(false);
   AnchorPane.setLeftAnchor(content1, 0.0);

    } 

Project snapshot

At start of project a make menubox.setVisible(false); but during running i want to make menubox set visible true but inside ContentNavigator.loadContent(ContentNavigator.CONTENT1);

CONTENT1 fxml contain a button on clicking that button i want menubox set visible true. tell me the way through which i can make it.

user3204934
  • 485
  • 6
  • 15
  • 1
    Surely you just want to expose the size of the panel, not the panel itself? There are many techniques for this: a great summary is at http://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/GraphicsContext.html – James_D Jun 12 '14 at 14:59
  • yes i want to get the size of panel and set for next window. but the problem is that when i make my panel `public static` it gives me an error – user3204934 Jun 12 '14 at 15:02
  • I don't understand why you would want to make the panel `public`, and certainly have no idea what this has to do with `static`. All you need to do is pass the new controller the current dimensions of the panel when you switch windows. Read the linked discussion for ways to do that. – James_D Jun 12 '14 at 15:05
  • so what would i do to get dimensions of the panel and pass it to next controller. – user3204934 Jun 12 '14 at 15:09
  • Sorry, contrived to paste the wrong link in. Read this: http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/ – James_D Jun 12 '14 at 15:11
  • i am new to javfx so all this thing are bouncing above my head can you explain it in simple way. – user3204934 Jun 12 '14 at 15:15
  • Edit your question with more details on how you are opening the new view from the current view (post some code). Not sure I can make it any simpler than it is in the link but I can try. – James_D Jun 12 '14 at 15:17

1 Answers1

2

Your structure is not really clear to me (is "masterboard controller" the same as DashboardController?), but I think I get enough of it.

In the masterboard controller, just create and expose a BooleanProperty to determine if the menu is showing or not:

private final BooleanProperty menuShowing = new SimpleBooleanProperty(this, "menuShowing", false);
public final void setMenuShowing(boolean showing) {
    menuShowing.set(showing);
}
public final boolean isMenuShowing() {
    return menuShowing.get();
}
public BooleanProperty menuShowingProperty() {
    return menuShowing ;
}

and then in your initialize method bind the visible property of the menu to the menuShowingProperty:

public void initialize() {
    menuBox.visibleProperty().bind(menuShowing);
}

Now when you load new content, you just have to give the controller for the content the reference to the menuShowing property. The easiest way to do this is something like:

// controller for individual content
public class ContentController {
    private BooleanProperty menuShowingProperty ;
    public void setMenuShowingProperty(BooleanProperty menuShowingProperty) {
        this.menuShowingProperty = menuShowingProperty ;
    }

    public void initialize() {
        // ...
    }

    // handler method for button that shows menu:
    @FXML
    private void showMenu() {
        menuShowingProperty.set(true);
    }
}

Now when you load the content, do something like:

public static void loadContent(String fxmlFile){
    try {
        FXMLLoader loader = new FXMLLoader(ContentNavigator.class.getResource(fxmlFile));
        AnchorPane pane = loader.<AnchorPane>load();
        Object controller = loader.getController();
        if (controller instanceof ContentController) {
            ContentController contentController = (ContentController) controller ;
            contentController.setMenuShowingProperty(mainController.menuShowingProperty());
        }
        mainController.setContent(pane);
    } catch (IOException ex) {
        Logger.getLogger(ContentNavigator.class.getName()).log(Level.SEVERE, null, ex);
    }
}

There are lots of other ways to handle this.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • but i want to know how you have done this. with only little data . – user3204934 Jun 12 '14 at 17:00
  • but i can't change `AnchorPane.setLeftAnchor(content1, 0.0);` to ` AnchorPane.setLeftAnchor(content1, 157.0);` – user3204934 Jun 12 '14 at 17:03
  • 2
    Don't hard-code the layout. `AnchorPane`s are rarely useful for anything that changes. Use something else, `BorderPane` might be best. That's really a different question entirely. – James_D Jun 12 '14 at 17:06
  • @user3204934 James can do it cause he's a telepathic genius and he has *lots* of experience developing and answering questions ;-) – jewelsea Jun 12 '14 at 18:24