Skip to content

Commit

Permalink
Merge pull request #33 from IDATT2003-Gruppe-53/32-view-manager
Browse files Browse the repository at this point in the history
Added a class for changing views/stages
  • Loading branch information
roaraf authored Apr 15, 2026
2 parents 0ae9629 + 3b4cec7 commit e30d761
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
3 changes: 3 additions & 0 deletions millions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>no.ntnu.gruppe53.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
25 changes: 20 additions & 5 deletions millions/src/main/java/no/ntnu/gruppe53/App.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package no.ntnu.gruppe53;

/**
* Hello world!
*/
public class App {
import javafx.application.Application;
import javafx.stage.Stage;
import no.ntnu.gruppe53.controller.ViewController;
import no.ntnu.gruppe53.views.StartView;


public class App extends Application {
@Override
public void start(Stage stage) {
ViewController vm = new ViewController(stage);

vm.addView("start", new StartView(vm));

vm.switchView("start");

stage.setTitle("Millions - the Stock Game");
stage.show();
}

public static void main(String[] args) {
System.out.println("Hello World!");
launch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package no.ntnu.gruppe53.controller;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

/**
* Manages application views and handles switching between them.
* <p>
* Stores a mapping of view names to JavaFX {@link Parent} nodes and updates
* the current Scene by replacing its root node when switching views.
* </p>
*/
public class ViewController {
/** Primary application window used to display the Scene. */
private Stage stage;
/** Scene containing the currently active view. */
private Scene scene;
/** Map of view identifiers to their corresponding UI roots. */
private Map<String, Parent> views = new HashMap<>();

/**
* Constructs a ViewController and initializes a new Scene.
*
* @param stage the primary application stage where the scene will be set
*/
public ViewController(Stage stage) {
this.stage = stage;
this.scene = new Scene(new javafx.scene.layout.StackPane(), 800, 600);
stage.setScene(scene);
}

/**
* Registers a view in the controller.
*
* @param name unique identifier used to reference the view later
* @param view the JavaFX root node representing the view
*/
public void addView(String name, Parent view) {
views.put(name, view);
}

/**
* Switches the currently displayed view by replacing the Scene root.
*
* @param name the identifier of the view to display
* @throws IllegalArgumentException if no view is registered under the given name
*/
public void switchView(String name) {
Parent view = views.get(name);

if (view == null) {
throw new IllegalArgumentException("No views found matching: " + name);
}

scene.setRoot(view);
}
}
11 changes: 11 additions & 0 deletions millions/src/main/java/no/ntnu/gruppe53/views/StartView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package no.ntnu.gruppe53.views;

import javafx.scene.layout.VBox;
import no.ntnu.gruppe53.controller.ViewController;

public class StartView extends VBox {

public StartView(ViewController vm) {

}
}

0 comments on commit e30d761

Please sign in to comment.