Created
December 2, 2013 11:46
-
-
Save skrb/7748343 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.List; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Node; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
public class MultiPageDemo extends Application { | |
private List<String> fxmls = Arrays.asList( | |
"page0.fxml", | |
"page1.fxml" | |
); | |
private int index = -1; | |
private StackPane root; | |
@Override | |
public void start(Stage stage) { | |
root = new StackPane(); | |
root.setPrefSize(400, 400); | |
forward(); | |
Scene scene = new Scene(root); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
// ページを進める | |
public void forward() { | |
try { | |
index++; | |
if (index >= fxmls.size()) { | |
index = 0; | |
} | |
// 次のページの読み込み | |
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmls.get(index))); | |
Node page = loader.load(); | |
// 現在ページが表示されていれば、それを削除 | |
if (!root.getChildren().isEmpty()) { | |
root.getChildren().remove(0); | |
} | |
// 次のページを表示 | |
root.getChildren().add(page); | |
// FXMLLoader からコントローラクラスを取得 | |
Pageable pageable = (Pageable) loader.getController(); | |
// コントローラクラスに自分自身を登録 | |
pageable.setMain(this); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void main(String... args) { | |
launch(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import java.lang.*?> | |
<?import java.util.*?> | |
<?import javafx.scene.*?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.layout.*?> | |
<StackPane prefHeight="400.0" prefWidth="400.0" style="-fx-background-color: red;" xmlns:fx="http://javafx.com/fxml" fx:controller="PageController0"> | |
<children> | |
<Button mnemonicParsing="false" onAction="#forward" text="Page 0" /> | |
</children> | |
</StackPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import java.lang.*?> | |
<?import java.util.*?> | |
<?import javafx.scene.*?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.layout.*?> | |
<StackPane id="AnchorPane" prefHeight="400.0" prefWidth="400.0" style="-fx-background-color: green;" xmlns:fx="http://javafx.com/fxml" fx:controller="PageController1"> | |
<children> | |
<Button mnemonicParsing="false" onAction="#forward" text="Page 1" /> | |
</children> | |
</StackPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Pageable { | |
public void setMain(MultiPageDemo main); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL; | |
import java.util.ResourceBundle; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
public class PageController0 implements Initializable, Pageable { | |
private MultiPageDemo main; | |
@FXML | |
private void forward(ActionEvent evet) { | |
main.forward(); | |
} | |
@Override | |
public void setMain(MultiPageDemo main) { | |
this.main = main; | |
} | |
@Override | |
public void initialize(URL url, ResourceBundle rb) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL; | |
import java.util.ResourceBundle; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
public class PageController1 implements Initializable, Pageable { | |
private MultiPageDemo main; | |
@FXML | |
private void forward(ActionEvent evet) { | |
main.forward(); | |
} | |
@Override | |
public void setMain(MultiPageDemo main) { | |
this.main = main; | |
} | |
@Override | |
public void initialize(URL url, ResourceBundle rb) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment