Added some javadoc, improved the readme.

This commit is contained in:
Andrew Lalis 2023-12-23 08:20:55 -05:00
parent bddea200fc
commit 9766e1ddee
3 changed files with 76 additions and 1 deletions

View File

@ -13,3 +13,73 @@ However, sometimes you'll want a web-like experience with your desktop app, and
for that purpose, I've created javafx-scene-router. It allows you to initialize
a router that controls the content of a Pane or similar, and depending on what
route is selected, different content will be shown in that pane.
# Usage
Add the following dependency to your `pom.xml`:
```xml
<dependency>
<groupId>com.andrewlalis</groupId>
<artifactId>javafx-scene-router</artifactId>
<version>LATEST_VERSION</version>
</dependency>
```
> Replace `LATEST_VERSION` with the most recent version found on [maven central](https://central.sonatype.com/artifact/com.andrewlalis/javafx-scene-router).
Then, most often you'll create a singleton instance of `SceneRouter`, probably
in your app's main class like so:
```java
import com.andrewlalis.javafx_scene_router.SceneRouter;
public class MyJavaFXApp extends Application {
public static final SceneRouter router = new SceneRouter();
public static void main(String[] args) {
// Setup the router's routes before starting the app:
router.map("accounts", MyJavaFXApp.class.getResource("/accounts-view.fxml"));
router.map("account", MyJavaFXApp.class.getResource("/account.fxml"));
router.map("settings", MyJavaFXApp.class.getResource("/settings.fxml"));
launch(args);
}
}
```
From here, it's just a matter of attaching the router's view pane to one of
your scene's nodes, and mapping string route names to other nodes or FXML
files.
For example, suppose my app has a main scene with a `MainController` class.
We'd hook up the router's view pane to that scene's center view:
```java
public class MainController {
@FXML
public BorderPane borderPane;
@FXML
public void initialize() {
borderPane.setCenter(MyJavaFXApp.router.getViewPane());
}
}
```
Finally, we can use the router from anywhere in our app to control which "page"
is being displayed:
```java
public class MainController {
// Rest of the class omitted.
@FXML
public void onBackButton() {
MyJavaFXApp.router.navigateBack();
}
@FXML
public void onAccountClicked() {
Account acc = getClickedAccount();
MyJavaFXApp.router.navigate("account", acc);
}
}
```

View File

@ -6,7 +6,7 @@
<groupId>com.andrewlalis</groupId>
<artifactId>javafx-scene-router</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<name>JavaFX Scene Router</name>
<description>A library that provides a router implementation for JavaFX, for browser-like navigation between pages.</description>
<url>https://github.com/andrewlalis/javafx-scene-router</url>

View File

@ -24,6 +24,11 @@ import java.util.function.Consumer;
* The router maintains a {@link RouteHistory} so that it's possible to
* navigate backward and forward, much like a web browser would.
* </p>
* <p>
* Note that this router is intended to be used by a single JavaFX
* application, and is <strong>not threadsafe!</strong> Use a separate
* router for each separate JavaFX application you create.
* </p>
*/
public class SceneRouter {
private final Pane viewPane = new Pane();