JDK8u40 で追加された Spinner と Dialog を試してみた。

JDK8u40 でやっと Dialog が追加されました。

これでお手軽に使えることになります。

あと Spinner も、そして Formatted Text も追加されました。

今回は Spinner と Dialog をサクッと使ってみたいと思います。

毎月のお小遣いの金額を Spinner で入力してある金額より低いもしくは高い場合に Dialog を出すという超シンプルなものです。

しかし、私はこれでドツボにはまり、未だに改善策を見いだせずに困っています。

心優しい優秀な Java プログラマのかたがこの記事を見ていたらどうか解決方法をお教えくださいませ。

Dialog の設定を Modality.APPLICATION_MODAL と Modality.WINDOW_MODAL にすると思わぬ事態が発生します。

それではプログラムのソースコード及び動画を貼っておきます。












    
        
      
      
    

package jp.yucchi.hellospinneranddialog;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Yucchi
 */
public class HelloSpinnerAndDialog extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle("毎月のお小遣いは?");
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}


package jp.yucchi.hellospinneranddialog;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;

/**
 *
 * @author Yucchi
 */
public class FXMLDocumentController implements Initializable {

    private BooleanProperty poor;

    private BooleanProperty rich;

    @FXML
    AnchorPane anchorPane;

    @FXML
    private Label label_1;

    @FXML
    private Spinner spinner;

    @FXML
    private Label label_2;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        poor = new SimpleBooleanProperty(true);
        rich = new SimpleBooleanProperty(true);

        // 0 から 100,000 まで 5,000 ずつ加減算する、初期値は 50,000 の Spinner
        spinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 100_000, 50_000, 5_000));
//        // Spinner のデザインを水平にする
//        spinner.getStyleClass().add(Spinner.STYLE_CLASS_ARROWS_ON_RIGHT_HORIZONTAL);

        spinner.valueProperty().addListener((ov, oldValue, newValue) -> {

            System.out.println(newValue);

            if (newValue <= 30_000 && poor.getValue()) {
                poor.setValue(Boolean.FALSE);
                showPoorDialog();
            }
            if (newValue >= 70_000 && rich.getValue()) {
                rich.setValue(Boolean.FALSE);
                showRichDialog();
            }
        });

    }

    private void showPoorDialog() {
        Alert alert = new Alert(AlertType.WARNING, "もう少しお小遣いを増やしましょう。", ButtonType.OK);
        alert.initModality(Modality.NONE);
        alert.initOwner(anchorPane.getScene().getWindow());
        alert.getDialogPane().setHeaderText("少ないですね。");
        alert.showAndWait()
                .filter(response -> response == ButtonType.OK)
                .ifPresent(response -> System.out.println(":p"));
    }

    private void showRichDialog() {
        Alert alert = new Alert(AlertType.INFORMATION, "お金で愛は買えないが、お金で愛は潤います。", ButtonType.OK);
        alert.initModality(Modality.NONE);
        alert.initOwner(anchorPane.getScene().getWindow());
        alert.getDialogPane().setHeaderText("幸せですね!");
        alert.showAndWait()
                .filter(response -> response == ButtonType.OK)
                .ifPresent(response -> System.out.println(":p"));
    }

}


Spinner の CSS のあてかたも解らないので入力値を右寄せにしたり色を変えたりとかもしたかったのですがそれはネット上に情報が出てきてからボチボチ勉強していきたいですね。

追記:キーボードからカーソルキー入力では問題ないのでバグの可能性が高いです。

Hatena タグ:

「JDK8u40 で追加された Spinner と Dialog を試してみた。」への2件のフィードバック

  1. ピンバック: ゆっちのBlog » JDK8u40 で追加された Spinner と Dialog を試してみた。 とりあえず回避方法をみつけた。

  2. ピンバック: ゆっちのBlog » JDK8u40 で追加された Formatted Text も試してみた。

コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です