In my application I have some components which are needed in different functions.
Right now every time i open a function, which needs one of the components, all components are built from the beginning.
This is unnecessary so I want to change this, but I don't know how.
The components, which I don't need anymore, should hide in the back and when they are needed again they should be put to the front.
This should improve the performance, since the components don't need to be rebuilt (or at least I hope so).
I hope someone has an idea of how I can solve this problem.
Asked
Active
Viewed 381 times
0

Yupp
- 315
- 3
- 18
-
[`Cell`](https://docs.oracle.com/javase/9/docs/api/javafx/scene/control/Cell.html) used e.g. with [`ListView`](https://docs.oracle.com/javase/9/docs/api/javafx/scene/control/ListView.html) is basically something that does this: `ListView` creates cells that are required to fill it's view and assigns the appropriate item to each cell. When the user scrolls `ListView` will assign different items to the cells. If the `ListView`'s size is increased, new cells are created as needed... – fabian Jan 29 '18 at 14:35
-
The way I read this, the user is almost looking for an anti-cell (e.g. a node that is retained in memory and possibly not displayed, rather than a node which is re-used for displaying different pieces of data). Perhaps he is looking for something like: [Javafx Stackpane show similar CardLayout Java Swing](https://stackoverflow.com/questions/40066655/javafx-stackpane-show-similar-cardlayout-java-swing). In any case, it's pretty vague ;-) – jewelsea Jan 30 '18 at 01:02
-
It's vague, but I don't think it's too broad. (Can you be vague, yet specific...? Maybe it is vaguely specific. ;).) It seems fairly clear to me that the OP is just looking for a way to retain a reference to something after it's been created once - i.e. they are looking for the lazy initialization pattern. – James_D Jan 30 '18 at 01:05
1 Answers
2
It sounds like all you need here is to lazily instantiate each piece you need, and keep a reference to it. The question is very general, so it's hard to give a concrete example, but the basic idea would look something like this:
public class SceneController {
private Parent view1 ;
private Parent view2 ;
// etc... You could perhaps store these in a map, or other data structure if needed
public Parent getView1() {
if (view1 == null) {
view1 = createView1();
}
return view1 ;
}
public Parent getView2() {
if (view2 == null) {
view2 = createView2();
}
return view2 ;
}
private Parent createView1() {
// Build first view. (Could be done by loading FXML, etc.)
}
private Parent createView2() {
// Build second view...
}
}
Then you can do things along the lines of
public class MyApp extends Application {
@Override
public void start(Stage primaryStage) {
SceneController sceneController = new SceneController();
BorderPane root = new BorderPane();
Button showView1 = new Button("View 1");
Button showView2 = new Button("View 2");
ButtonBar buttonBar = new ButtonBar();
buttonBar.getButtons().addAll(showView1, showView2);
root.setTop(buttonBar);
showView1.setOnAction(e -> root.setCenter(sceneController.getView1()));
showView2.setOnAction(e -> root.setCenter(sceneController.getView2()));
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}

James_D
- 201,275
- 16
- 291
- 322