仕事でJAVAFXのウィンドウを作る事になりました。
上にタイトル
真ん中にチェックボックスとテキストのは位置、中身は可変で、スクロールできる。
下部にOKキャンセルボタンを配置
ソースコードは以下
import java.util.List;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class test_2 extends Application {
final static double WIDTH = 400;
final static double HEIGHT = 700;
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("タイトル");
BorderPane borderPane = new BorderPane();
borderPane.setTop(setTitle());
borderPane.setCenter(setbody());
Button okButton = new Button("OK");
okButton.setPadding(new Insets(5));
Button cancelButton = new Button("cancel");
cancelButton.setPadding(new Insets(5));
HBox hbox = new HBox(10);
hbox.setPadding(new Insets(20));
Pane bottunspan = new Pane();
//bottunspan.setPrefHeight(10);
//bottunspan.setPrefWidth(40);
hbox.getChildren().addAll(cancelButton ,bottunspan,okButton);
hbox.setAlignment(Pos.CENTER);
borderPane.setBottom(hbox);
cancelButton.setOnAction((ActionEvent) ->{
stage.close();
});
okButton.setOnAction((ActionEvent) ->{
ScrollPane scrollPane = (ScrollPane) borderPane.centerProperty().getValue();
VBox chekboxitems = (VBox) scrollPane.getContent();
int checkboxClassRow = 2;
for(int i = 0 ; i < chekboxitems.getChildren().size() ;i++) {
HBox chekboxitem = (HBox) chekboxitems.getChildren().get(i);
CheckBox checkBox = (CheckBox) chekboxitem.getChildren().get(checkboxClassRow);
if(checkBox.isSelected()) {
System.out.println(chekboxitem.getChildren().get(0));
}
}
});
Scene scene = new Scene(borderPane,WIDTH,HEIGHT);
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
private static ScrollPane setbody() {
VBox selectBoxes = new VBox(8);
String colorGray = "#DDDDDD";
for(int i = 0 ; i < 20 ; i++) {
Label checkBoxLabel = new Label("チェックボックス"+i);
//checkBoxLabel.setPadding(new Insets(0));
CheckBox checkBox = new CheckBox();
//checkBox.setPadding(new Insets(5));
HBox hbox = new HBox(5);
hbox.setPrefWidth(WIDTH-20);
hbox.setPadding(new Insets(5));
//テキストとチェックボックスを両端にするためのスペース用
Pane space = new Pane();
hbox.setHgrow(space, Priority.ALWAYS);
if(i % 2 == 1) {
//title.setTextFill(Color.WHITE);
hbox.setStyle("-fx-background-color:"+colorGray+";");
}
hbox.getChildren().addAll(checkBoxLabel,space,checkBox);
selectBoxes.getChildren().add(hbox);
}
selectBoxes.setPadding(new Insets(5));
ScrollPane s1 = new ScrollPane();
s1.setContent(selectBoxes);
s1.setPrefWidth(WIDTH-30);
return s1;
}
private static HBox setTitle() {
Label title = new Label("表題");
//title.setStyle("-fx-font-size: 30px; -fx-background-color:#DDDDDD;");
int fontSize = 30;
title.setStyle("-fx-font-size: "+fontSize+"px;");
title.setTextFill(Color.WHITE);
HBox titleBox = new HBox(8);
titleBox.getChildren().add(title);
String colorWhite = "#008000";
titleBox.setStyle("-fx-background-color:" + colorWhite + ";");
titleBox.setPadding(new Insets(10));
titleBox.setMinHeight(60);
titleBox.setMinWidth(WIDTH);
return titleBox;
}
public static void main(String[] args) {
launch(args);
}
}
以下公式チュートリアルリンク
