Simple GIS

GISプログラムの練習

table

package fx01;
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;


public class pro extends Application {

    private final ObservableList data =
            FXCollections.observableArrayList(
            new Standings("ソフトバンク", 24, 18, 4, 2, 0.818),
            new Standings("オリックス", 24, 15, 7, 2, 0.682),
            new Standings("日本ハム", 24, 16, 8, 0, 0.667),
            new Standings("中日", 24, 14, 10, 0, 0.583),
            new Standings("西武", 24, 12, 11, 1, 0.522),
            new Standings("ヤクルト", 24, 10, 12, 2, 0.455),
            new Standings("巨人", 24, 10, 13, 1, 0.435),
            new Standings("阪神", 24, 10, 14, 0, 0.417),
            new Standings("楽天", 24, 9, 13, 2, 0.409),
            new Standings("ロッテ", 24, 8, 14, 2, 0.364),
            new Standings("横浜", 24, 7, 13, 4, 0.350),
            new Standings("広島", 24, 6, 16, 2, 0.273));

    @Override
    public void start(Stage primaryStage) {
        TableView tbl = new TableView<>();
        tbl.setEditable(false);

        TableColumn teamNameCol = new TableColumn("チーム");
        teamNameCol.setMinWidth(120);
        teamNameCol.setCellValueFactory(
                new PropertyValueFactory<Standings, String>("teamName"));

        TableColumn gameNumCol = new TableColumn("試合数");
        gameNumCol.setMinWidth(60);
        tableCellAlignRight(gameNumCol);
        gameNumCol.setCellValueFactory(
                new PropertyValueFactory<Standings, Integer>("gameNum"));

        TableColumn winNumCol = new TableColumn("勝数");
        winNumCol.setMinWidth(60);
        tableCellAlignRight(winNumCol);
        winNumCol.setCellValueFactory(
                new PropertyValueFactory<Standings, Integer>("winNum"));

        TableColumn lostNumCol = new TableColumn("負数");
        lostNumCol.setMinWidth(60);
        tableCellAlignRight(lostNumCol);
        lostNumCol.setCellValueFactory(
                new PropertyValueFactory<Standings, Integer>("lostNum"));

        TableColumn drawNumCol = new TableColumn("引分数");
        drawNumCol.setMinWidth(60);
        tableCellAlignRight(drawNumCol);
        drawNumCol.setCellValueFactory(
                new PropertyValueFactory<Standings, Integer>("drawNum"));

        TableColumn winRatioCol = new TableColumn("勝率");
        winRatioCol.setMinWidth(60);
        tableCellAlignRight(winRatioCol);
        winRatioCol.setCellValueFactory(
                new PropertyValueFactory<Standings, Double>("winRatio"));

        tbl.getColumns().addAll(teamNameCol, gameNumCol, winNumCol, lostNumCol,
                drawNumCol, winRatioCol);
        tbl.setItems(data);

        StackPane root = new StackPane();
        root.getChildren().add(tbl);

        Scene scene = new Scene(root);

        primaryStage.setTitle("Sample_TableView");
        primaryStage.setScene(scene);
        primaryStage.setWidth(500);
        primaryStage.setHeight(400);
        primaryStage.show();
    }

    
    public void tableCellAlignRight(TableColumn col) {
        col.setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                TableCell cell = new TableCell() {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        if (item != null) {
                            setText(item.toString());
                        }
                    }
                };
                cell.setAlignment(Pos.BOTTOM_RIGHT);
                return cell;
            }
        });
    }

    public static class Standings {
        private final SimpleStringProperty teamName;
        private final SimpleIntegerProperty gameNum;
        private final SimpleIntegerProperty winNum;
        private final SimpleIntegerProperty lostNum;
        private final SimpleIntegerProperty drawNum;
        private final SimpleDoubleProperty winRatio;

        private Standings(String tName, int gameN, int winN, int lostN,
                int drawN, double winR) {
            this.teamName = new SimpleStringProperty(tName);
            this.gameNum = new SimpleIntegerProperty(gameN);
            this.winNum = new SimpleIntegerProperty(winN);
            this.lostNum = new SimpleIntegerProperty(lostN);
            this.drawNum = new SimpleIntegerProperty(drawN);
            this.winRatio = new SimpleDoubleProperty(winR);
        }

        // チーム
        public String getTeamName() {
            return teamName.get();
        }

        public void setTeamName(String tName) {
            teamName.set(tName);
        }

        // 試合数
        public int getGameNum() {
            return gameNum.get();
        }

        public void setGameNum(int gameN) {
            gameNum.set(gameN);
        }

        // 勝数
        public int getWinNum() {
            return winNum.get();
        }

        public void setWinNum(int winN) {
            winNum.set(winN);
        }

        // 負数
        public int getLostNum() {
            return lostNum.get();
        }

        public void setLostNum(int lostN) {
            lostNum.set(lostN);
        }

        // 引分数
        public int getDrawNum() {
            return drawNum.get();
        }

        public void setDrawNum(int drawN) {
            lostNum.set(drawN);
        }

        // 勝率
        public double getWinRatio() {
            return winRatio.get();
        }

        public void setWinRatio(double winR) {
            winRatio.set(winR);
        }
    }


    public static void main(String[] args) {
        launch(args);
    }
}