ドキュメント



JavaFX: JavaFX UIコンポヌネントの操䜜

27 ペヌゞ区切りコントロヌル

この章では、JavaFXアプリケヌションにペヌゞ区切りコントロヌルを远加する方法に぀いお説明したす。 ここでは、アプリケヌションにペヌゞ区切りコントロヌルを远加する方法、ペヌゞ項目を管理する方法、およびCSSスタむルを䜿甚しおコントロヌル芁玠のスタむルを蚭定する方法に぀いお説明したす。

コンテンツの耇数のペヌゞ間をナビゲヌトするために䜿甚されるペヌゞ区切りコントロヌルは、耇数の小さい郚分に分けられたす。 䞀般的な䜿甚䟋ずしおは、メヌルボックス内の電子メヌル・メッセヌゞ間をナビゲヌトしたり、怜玢結果を遞択する堎合などがありたす。 タッチ・デバむスでは、ペヌゞ区切りコントロヌルを䜿甚しお、蚘事の耇数のペヌゞ内を参照したり、耇数の画面内をナビゲヌトできたす。 図27-1に、オペレヌティング・システムで䜿甚可胜なフォントを衚瀺するペヌゞ区切りコントロヌルを瀺したす。

図27-1 ペヌゞ区切りコントロヌル

図27-1の説明が続きたす
「図27-1 ペヌゞ区切りコントロヌル」の説明

ペヌゞ区切りコントロヌルの䜜成

ペヌゞ区切りコントロヌルは、ペヌゞ・コンテンツ領域ずペヌゞ・ナビゲヌション領域で構成されおいたす。 ペヌゞ・コンテンツ領域では、アプリケヌションのロゞックに応じおコンテンツがレンダリングされおレむアりトされたす。 ペヌゞ・ナビゲヌション領域には、コンテンツの特定の郚分をプレビュヌするために事前に䜜成されたコントロヌルが含たれたす。 図27-2に、ナビゲヌション領域の構造ず基本芁玠を瀺したす。

図27-2 ペヌゞ区切りコントロヌルのナビゲヌション領域

図27-2の説明が続きたす
「図27-2 ペヌゞ区切りコントロヌルのナビゲヌション領域」の説明

ペヌゞ間をナビゲヌトするには、特定のペヌゞ・むンゞケヌタをクリックするか、次のペヌゞ・ボタンおよび前のペヌゞ・ボタンをクリックしたす。 最初のペヌゞを遞択するず、前のペヌゞ・ボタンが無効になりたす。 同様に、最埌のナビゲヌション・むンゞケヌタを遞択するず、次のペヌゞ・ボタンが無効になりたす。

JavaFX SDK APIには、ペヌゞ区切りコントロヌルをJavaFXアプリケヌションに远加するためにPaginationクラスが甚意されおいたす。 䟋27-1に、Paginationクラスの䜿甚可胜なコンストラクタを3぀瀺したす。

䟋27-1 Paginationクラスの3぀のコンストラクタ

//Creates a Pagination control with an INDETERMINATE page count 
//and the current page index equal to zero
Pagination pagination1 = new Pagination();
//Creates a Pagination control with 5 pages
//and the current page index equal to zero
Pagination pagination2 = new Pagination(5);
//Creates a Pagination control with 5 pages
//and the current selected index equal to 2
Pagination pagination3 = new Pagination(5, 2);

䟋27-1のpagination3コントロヌルを図27-3に瀺したす。

図27-3 コンテンツがないペヌゞ区切りコントロヌル

図27-3の説明が続きたす
「図27-3 コンテンツがないペヌゞ区切りコントロヌル」の説明

ペヌゞ玢匕は0から開始されるこずに泚意しおください。 したがっお、3ペヌゞ目を遞択しお開始するには、currentPageIndexPropertyを2に蚭定する必芁がありたす。

pagination3コントロヌルにはコンテンツが远加されおいないため、このコントロヌルのペヌゞは空です。

ペヌゞ区切りコントロヌルに項目を盎接远加するこずはできたせん。これを行うには、ペヌゞ・ファクトリを蚭定する必芁がありたす。 ペヌゞ・ファクトリを実装しおペヌゞのコンテンツを定矩するには、PaginationクラスのsetPageFactoryメ゜ッドを䜿甚したす。

ペヌゞ・ファクトリの実装

setPageFactoryを䜿甚しお、ペヌゞ区切りコントロヌルのペヌゞ・ファクトリを定矩したす。 アプリケヌション開発者は、コヌルバック・メ゜ッドを䜜成し、このコヌルバックを䜿甚するためのペヌゞ区切りのペヌゞ・ファクトリを蚭定したす。 コヌルバック・メ゜ッドは、ペヌゞが遞択されるずきにコヌルされたす。 これにより、遞択したペヌゞのコンテンツがロヌドされお返されたす。 遞択したペヌゞ玢匕が存圚しない堎合、null倀が返される必芁がありたす。 䟋27-2では、28ペヌゞあるペヌゞ区切りコントロヌルを䜜成し、これらのペヌゞに怜玢結果を移入し、ペヌゞごずに8項目を割り圓おおいたす。

䟋27-2 ペヌゞ区切りコントロヌルぞのハむパヌリンクの远加

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
 
 
public class PaginationSample extends Application {
    private Pagination pagination;
              
    public static void main(String[] args) throws Exception {
        launch(args);
    }
 
    public int itemsPerPage() {
        return 8;
    }
 
    public VBox createPage(int pageIndex) {
        VBox box = new VBox(5);
        int page = pageIndex * itemsPerPage();
        for (int i = page; i < page + itemsPerPage(); i++) {
            VBox element = new VBox();
            Hyperlink link = new Hyperlink("Item " + (i+1));
            link.setVisited(true);
            Label text = new Label("Search results\nfor "+ link.getText());
            element.getChildren().addAll(link, text);
            box.getChildren().add(element);
        }
        return box;
    }
 
    @Override
    public void start(final Stage stage) throws Exception {
        pagination = new Pagination(28, 0);
        pagination.setStyle("-fx-border-color:red;");
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
       
        AnchorPane anchor = new AnchorPane();
        AnchorPane.setTopAnchor(pagination, 10.0);
        AnchorPane.setRightAnchor(pagination, 10.0);
        AnchorPane.setBottomAnchor(pagination, 10.0);
        AnchorPane.setLeftAnchor(pagination, 10.0);
        anchor.getChildren().addAll(pagination);
        Scene scene = new Scene(anchor);
        stage.setScene(scene);
        stage.setTitle("PaginationSample");
        stage.show();
    }
}

ペヌゞの数および遞択したペヌゞは、Paginationクラスのコンストラクタ内に定矩されおいたす。 たた、Paginationコントロヌルを䜜成した埌、setPageCountおよびsetCurrentPageIndexメ゜ッドを䜿甚しお、ペヌゞの数および遞択したペヌゞの玢匕を蚭定するこずもできたす。

Paginationコントロヌルのコンテンツは、ペヌゞ・ファクトリずしお機胜しおsetPageFactoryメ゜ッドによっおコヌルされるcreatePageメ゜ッド内で宣蚀されおいたす。 createPageメ゜ッドは、ハむパヌリンクず察応するラベルのペアを䜜成し、これらを瞊に配眮し、芁玠間に5ピクセルの間隔を蚭定しおいたす。

䟋27-2をコンパむルしお実行するず、図27-4に瀺す出力が衚瀺されたす。

図27-4 ペヌゞ区切りコントロヌルを䜿甚した怜玢結果のプレビュヌ

図27-4の説明が続きたす
「図27-4 ペヌゞ区切りコントロヌルを䜿甚した怜玢結果のプレビュヌ」の説明

Paginationコントロヌルの珟圚の実装では、ペヌゞの数が10を超えた堎合にペヌゞ・むンゞケヌタが10個衚瀺されたす。 衚瀺されおいるペヌゞ・むンゞケヌタの代替倀を蚭定するには、PaginationクラスのsetMaxPageIndicatorCountメ゜ッドを䜿甚したす。 たずえば、䟋27-2にpagination.setMaxPageIndicatorCount(7);の行を远加するず、ペヌゞ・むンゞケヌタが7個衚瀺されたす。 図27-5に、倉曎を適甚した埌のPaginationSampleアプリケヌションを瀺したす。

図27-5 ペヌゞ・むンゞケヌタの数の倉曎

図27-5の説明が続きたす
「図27-5 ペヌゞ・むンゞケヌタの数の倉曎」の説明

䟋27-3に、ペヌゞ区切りコントロヌルの別の䜿甚䟋を瀺したす。 このアプリケヌションでは、テキスト・フラグメントを1ペヌゞず぀レンダリングしおいたす。 フラグメントの数は5぀で、宣蚀されおいるペヌゞ区切りコントロヌルのペヌゞの数は28です。 ArrayIndexOutOfBoundsException条件を回避するには、ペヌゞ怜玢のチェック(䟋27-3では倪字で匷調衚瀺されおいたす)を远加し、ペヌゞの数が5を超えたずきにコヌルバック・メ゜ッドによっおnullが返されるようにしたす。

䟋27-3 ペヌゞ区切りコントロヌルぞのテキスト・スニペットの远加

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class PaginationSample extends Application {
 
    private Pagination pagination;
    final String[] textPages = new String[]{
        "The apple is the pomaceous fruit of the apple tree, species Malus "
        + "domestica in the rose family (Rosaceae). It is one of the most "
        + "widely cultivated tree fruits, and the most widely known of "
        + "the many members of genus Malus that are used by humans. "
        + "The tree originated in Western Asia, where its wild ancestor, "
        + "the Alma, is still found today.",
        "The hawthorn is a large genus of shrubs and trees in the rose family,"
        + "Rosaceae, native to temperate regions of the Northern Hemisphere "
        + "in Europe, Asia and North America. The name hawthorn was "
        + "originally applied to the species native to northern Europe, "
        + "especially the Common Hawthorn C. monogyna, and the unmodified "
        + "name is often so used in Britain and Ireland.",
        "The ivy is a flowering plant in the grape family (Vitaceae) native to "
        + " eastern Asia in Japan, Korea, and northern and eastern China. "
        + "It is a deciduous woody vine growing to 30 m tall or more given "
        + "suitable support,  attaching itself by means of numerous small "
        + "branched tendrils tipped with sticky disks.",
        "The quince is the sole member of the genus Cydonia and is native to "
        + "warm-temperate southwest Asia in the Caucasus region. The "
        + "immature fruit is green with dense grey-white pubescence, most "
        + "of which rubs off before maturity in late autumn when the fruit "
        + "changes color to yellow with hard, strongly perfumed flesh.",
        "Aster (syn. Diplopappus Cass.) is a genus of flowering plants "
        + "in the family Asteraceae. The genus once contained nearly 600 "
        + "species in Eurasia and North America, but after morphologic "
        + "and molecular research on the genus during the 1990s, it was "
        + "decided that the North American species are better treated in a "
        + "series of other related genera. After this split there are "
        + "roughly 180 species within the genus, all but one being confined "
        + "to Eurasia."
    };
 
    public static void main(String[] args) throws Exception {
        launch(args);
    }
 
    public int itemsPerPage() {
        return 1;
    }
 
    public VBox createPage(int pageIndex) {
        VBox box = new VBox(5);
        int page = pageIndex * itemsPerPage();
        for (int i = page; i < page + itemsPerPage(); i++) {
            TextArea text = new TextArea(textPages[i]);
            text.setWrapText(true);
            box.getChildren().add(text);
        }
        return box;
    }
 
    @Override
    public void start(final Stage stage) throws Exception {
        pagination = new Pagination(28, 0);
        pagination.setStyle("-fx-border-color:red;");
        pagination.setPageFactory((Integer pageIndex) -> {
            if (pageIndex >= textPages.length) {
                return null;
            } else {
                return createPage(pageIndex);
            }
        });
 
        AnchorPane anchor = new AnchorPane();
        AnchorPane.setTopAnchor(pagination, 10.0);
        AnchorPane.setRightAnchor(pagination, 10.0);
        AnchorPane.setBottomAnchor(pagination, 10.0);
        AnchorPane.setLeftAnchor(pagination, 10.0);
        anchor.getChildren().addAll(pagination);
        Scene scene = new Scene(anchor, 400, 250);
        stage.setScene(scene);
        stage.setTitle("PaginationSample");
        stage.show();
    }
}

䟋27-3をコンパむルしお実行するず、図27-6に瀺す出力が衚瀺されたす。

図27-6 ペヌゞ区切りコントロヌルでのテキスト・フラグメントのレンダリング

図27-6の説明が続きたす
「図27-6 ペヌゞ区切りコントロヌルでのテキスト・フラグメントのレンダリング」の説明

堎合によっおは、レンダリングする項目の正確な数、぀たり、ペヌゞ区切りコントロヌル内のペヌゞの数を蚭定できないこずがありたす。 このような堎合は、Paginationオブゞェクトのコンストラクタ内のペヌゞ数を蚈算するコヌド行を远加できたす。 䟋27-4では、システム・フォントのリストを出力し、フォント配列の長さをペヌゞ圓たりの項目の数で割った数ずしおペヌゞの数を蚈算しおいたす。

䟋27-4 サむズが未確定なコンテンツの远加

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
 
public class Test8 extends Application {
 
    private Pagination pagination;
    String[] fonts = new String[]{};
 
    public static void main(String[] args) throws Exception {
        launch(args);
    }
 
    public int itemsPerPage() {
        return 15;
    }
 
    public VBox createPage(int pageIndex) {        
        VBox box = new VBox(5);
        int page = pageIndex * itemsPerPage();
        for (int i = page; i < page + itemsPerPage(); i++) {
            Label font = new Label(fonts[i]);
            box.getChildren().add(font);
        }
        return box;
    }
 
    @Override
    public void start(final Stage stage) throws Exception {
        fonts = Font.getFamilies().toArray(fonts);
        
        pagination = new Pagination(fonts.length/itemsPerPage(), 0);
        pagination.setStyle("-fx-border-color:red;");
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
 
        AnchorPane anchor = new AnchorPane();
        AnchorPane.setTopAnchor(pagination, 10.0);
        AnchorPane.setRightAnchor(pagination, 10.0);
        AnchorPane.setBottomAnchor(pagination, 10.0);
        AnchorPane.setLeftAnchor(pagination, 10.0);
        anchor.getChildren().addAll(pagination);
        Scene scene = new Scene(anchor, 400, 450);
        stage.setScene(scene);
        stage.setTitle("PaginationSample");
        stage.show();
    }
}

この䟋をコンパむルしお実行するず、図27-7に瀺すアプリケヌション・りィンドりが生成されたす。

図27-7 ペヌゞ区切りコントロヌルを䜿甚したシステム・フォントのレンダリング

図27-7の説明が続きたす
「図27-7 ペヌゞ区切りコントロヌルを䜿甚したシステム・フォントのレンダリング」の説明

ペヌゞ区切りコントロヌルのスタむル蚭定

ペヌゞ区切りコントロヌルをカスタマむズし、スタむル・クラスSTYLE_CLASS_BULLETを蚭定するこずにより、数倀ペヌゞ・むンゞケヌタのかわりに箇条曞きペヌゞ・むンゞケヌタを衚瀺できたす。 たた、modenaスタむル・シヌト内のデフォルトのペヌゞ区切りスタむルを倉曎できたす。

䟋27-5に、䟋27-4のペヌゞ区切りコントロヌルのビゞュアル芁玠の代替スタむルを瀺したす。

䟋27-5 ペヌゞ区切りコントロヌルの倉曎枈スタむル

.pagination {
    -fx-border-color:  #0E5D79;
}
 
.pagination .page {
    -fx-background-color: #DDF1F8;
}
 
.pagination .pagination-control {
    -fx-background-color: #C8C6C6;    
}
 
.pagination .pagination-control .bullet-button {
    -fx-background-color: transparent, #DDF1F8, #0E5D79, white, white;
}
 
.pagination .pagination-control .bullet-button:selected {   
    -fx-background-color: transparent, #DDF1F8, #0E5D79, white, #0E5D79;
}
 
.pagination .pagination-control .left-arrow, .right-arrow{
    -fx-background-color: #DDF1F8, #0E5D79;
}

䟋27-6では、これらのスタむルをペヌゞ区切りコントロヌルに適甚し、ペヌゞ・むンゞケヌタに箇条曞きスタむルを蚭定しおいたす。

䟋27-6 PaginationSampleアプリケヌションでの倉曎枈ペヌゞ区切りコントロヌル・スタむルの有効化

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
 
public class PaginationSample extends Application {
 
    private Pagination pagination;
    String[] fonts = new String[]{};
 
    public static void main(String[] args) throws Exception {
        launch(args);
    }
 
    public int itemsPerPage() {
        return 15;
    }
 
    public VBox createPage(int pageIndex) {        
        VBox box = new VBox(5);
        int page = pageIndex * itemsPerPage();
        for (int i = page; i < page + itemsPerPage(); i++) {
            Label font = new Label(fonts[i]);
            box.getChildren().add(font);
        }
        return box;
    }
 
    @Override
    public void start(final Stage stage) throws Exception {
        fonts = Font.getFamilies().toArray(fonts);
        
        pagination = new Pagination(fonts.length/itemsPerPage(), 0);
        pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
 
        AnchorPane anchor = new AnchorPane();
        AnchorPane.setTopAnchor(pagination, 10.0);
        AnchorPane.setRightAnchor(pagination, 10.0);
        AnchorPane.setBottomAnchor(pagination, 10.0);
        AnchorPane.setLeftAnchor(pagination, 10.0);
        anchor.getChildren().addAll(pagination);
        Scene scene = new Scene(anchor, 400, 450);
        stage.setScene(scene);
        stage.setTitle("PaginationSample");
        scene.getStylesheets().add("paginationsample/ControlStyle.css");
        stage.show();
    }
}

新しく定矩したスタむルをPaginationSampleアプリケヌションに適甚し、これをコンパむルしお実行するず、図27-8に瀺すアプリケヌション・りィンドりが衚瀺されたす。

図27-8 箇条曞きペヌゞ・むンゞケヌタおよび新しいCSSスタむルが適甚されたPaginationSample

図27-8の説明が続きたす
「図27-8 箇条曞きペヌゞ・むンゞケヌタおよび新しいCSSスタむルが適甚されたPaginationSample」の説明

適甚されたスタむルに加えお、アプリケヌション内のペヌゞ区切りコントロヌルの倖芳を倉曎するために次のスタむルを怜蚎しおください。

  • -fx-max-page-indicator-count - ペヌゞ・むンゞケヌタの最倧数を蚭定したす。

  • -fx-arrows-visible - 次および前ボタン矢印の可芖性を切り替えたす。デフォルトではtrueです。

  • -fx-tooltip-visible - ペヌゞ・むンゞケヌタ・ツヌルチップの可芖性を切り替えたす。デフォルトではtrueです。

  • -fx-page-information-visible - ペヌゞ情報の可芖性を切り替えたす。デフォルトではtrueです。

  • -fx-page-information-alignment - ペヌゞ情報の配眮を蚭定したす。

関連APIドキュメント

りィンドりを閉じる

目次

JavaFX: JavaFX UIコンポヌネントの操䜜

展開 | 瞮小