During this lesson we will create an application that contains 2 windows. First window is going to have an impact on the second one in dynamic way.
Lesson 12
During this lesson we will create an application that contains 2 windows. First window is going to have an impact on the second one in dynamic way.
A single object which is accessible everywhere in a project.
It is perceived as anti-pattern.
Huge companies (Apple, RedHat etc.) often use this anti-pattern. It is hard not to come across it.
Note that a field is static and constructor is private(!):
public class SlidesApplication {
private static SlidesApplication instance;
public static SlidesApplication getInstance() {
if (instance == null) {
instance = new SlidesApplication();
}
return instance;
}
private SlidesApplication() {
}
}
Class represents one slide (omitting GUI):
public class Slide {
private String message;
private String title;
public Slide(String message, String title) {
this.message = message;
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Let's modify main class of the application:
public class SlidesApplication {
private static SlidesApplication instance;
public static SlidesApplication getInstance() {
if (instance == null) {
instance = new SlidesApplication();
}
return instance;
}
private SlidesApplication() {
stageMap = new HashMap<>();
slideData = new ArrayList<>();
currentSlide = 0;
initSlidesData();
}
private Map<String, Stage> stageMap;
private int currentSlide;
private List<Slide> slideData;
public void registerStage(String name, Stage stage) {
stageMap.put(name, stage);
}
public void loadScene(String stageName, Scene scene) {
Stage stage = stageMap.get(stageName);
stage.setScene(scene);
}
private void initSlidesData() {
slideData.add(new Slide("Hint for a speaker 0", "Slide 0"));
slideData.add(new Slide("Hint for a speaker 1", "Slide 1"));
slideData.add(new Slide("Hint for a speaker 2", "Slide 2"));
}
public Scene renderScene() {
Slide current = slideData.get(currentSlide);
FXMLLoader loader = new FXMLLoader(getClass().getResource("/slide.fxml"));
SlideController controller = new SlideController(current);
loader.setController(controller);
try {
Pane pane = loader.load();
return new Scene(pane);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void createSlideStage() {
Stage stage = new Stage();
stage.setTitle("SlideStage");
stage.setScene(renderScene());
stage.show();
registerStage("SlideStage", stage);
}
// TODO Change index of the current slide
// TODO Return a scene with current slide view
public Scene turnLeft() {
return null;
}
// TODO Same like turnLeft(), but in different direction
public Scene turnRight() {
return null;
}
// TODO We have to return an information about a hint for a speaker
public String currentMessage() {
return null;
}
}
Analyze the code from SlidesApplication class, next add the missing code.
This time there is too much code to copy. Below you will find a link to download src catalog for your project. There are to pairs of files (.java + .fxml) for two windows.
Download source codeMake sure that there are no exceptions thrown during testing an application.
Modify a project: