JavaFx tableview設定セル
31068 ワード
問題のインポート:
JavaFxでは、Table Viewの使用中にセルデータのインポートに問題が発生しました.次に、Table Viewのインポートデータをまとめます.
具体的な手順
1.FXMLでTable Viewビューを作成する
2.宣言表の各列
3.データの挿入
2つの方法:
(1)モデルクラスの属性に応じて対応する列
モデルクラス:
(2)元の挿入方式
setCellValueFactoryメソッドを書き換え、Integerの場合、double汎用統合はNumberを使用し、paramは現在のモデルクラスの指向であることに注意します.
4.入力データ
JavaFxでは、Table Viewの使用中にセルデータのインポートに問題が発生しました.次に、Table Viewのインポートデータをまとめます.
具体的な手順
1.FXMLでTable Viewビューを作成する
<TableView fx:id="ranking_table" prefHeight="447.0" prefWidth="781.0" stylesheets="@../css/fullpackstyling.css" HBox.hgrow="ALWAYS">
<columns>
<TableColumn fx:id="Name" text=" " />
<TableColumn fx:id="Department" text=" " />
<TableColumn fx:id="Age" text=" " />
<TableColumn fx:id="Run3km" text="3KM" />
<TableColumn fx:id="Score1" text=" " />
<TableColumn fx:id="Sit_ups" prefWidth="84.0" text=" " />
<TableColumn fx:id="Score2" prefWidth="46.0" text=" " />
<TableColumn fx:id="horizontal_bar" text=" " />
<TableColumn fx:id="Score3" text=" " />
<TableColumn fx:id="Serpentine_run" text=" " />
<TableColumn fx:id="Score4" text=" " />
<TableColumn fx:id="TotalScore" text=" " />
<TableColumn fx:id="Ranking" text=" " />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
2.宣言表の各列
@FXML
private TableColumn<Scores, String> Name = new TableColumn<Scores, String>(
" ");
@FXML
private TableColumn<Scores, String> Department = new TableColumn<Scores, String>(
" ");
@FXML
private TableColumn<Scores, Integer> Age = new TableColumn<Scores, Integer>(
" ");
@FXML
private TableColumn<Scores, String> Run3km = new TableColumn<Scores, String>(
"3KM");
@FXML
private TableColumn<Scores, Integer> Score1 = new TableColumn<Scores, Integer>(
" 1");
@FXML
private TableColumn<Scores, Integer> Sit_ups = new TableColumn<Scores, Integer>(
" ");
@FXML
private TableColumn<Scores, Integer> Score2 = new TableColumn<Scores, Integer>(
" 2");
@FXML
private TableColumn<Scores, Integer> horizontal_bar = new TableColumn<Scores, Integer>(
" ");
@FXML
private TableColumn<Scores, Integer> Score3 = new TableColumn<Scores, Integer>(
" 3");
@FXML
private TableColumn<Scores, String> Serpentine_run = new TableColumn<Scores, String>(
" ");
@FXML
private TableColumn<Scores, Integer> Score4 = new TableColumn<Scores, Integer>(
" 4");
@FXML
private TableColumn<Scores, Integer> TotalScore = new TableColumn<Scores, Integer>(
" ");
@FXML
private TableColumn<Scores, Number> Ranking = new TableColumn<Scores, Number>(
" ");
3.データの挿入
2つの方法:
(1)モデルクラスの属性に応じて対応する列
モデルクラス:
private Students students;
private String Name;
private String Department;
private int Age;
private String Run3km;
private int Score1;
private int Sit_ups;
private int Score2;
private int horizontal_bar;
private int Score3;
private String Serpentine_run;
private int Score4;
private int TotalScore;
private int Ranking;
private int data;
Name.setCellValueFactory(new PropertyValueFactory<>("Name"));
Department.setCellValueFactory(new PropertyValueFactory<>("Department"));
Age.setCellValueFactory(new PropertyValueFactory<>("Age"));
Run3km.setCellValueFactory(new PropertyValueFactory<>("Run3km"));
Score1.setCellValueFactory(new PropertyValueFactory<>("Score1"));
Sit_ups.setCellValueFactory(new PropertyValueFactory<>("Sit_ups"));
Score2.setCellValueFactory(new PropertyValueFactory<>("Score2"));
horizontal_bar.setCellValueFactory(new PropertyValueFactory<>("horizontal_bar"));
Score3.setCellValueFactory(new PropertyValueFactory<>("Score3"));
Serpentine_run.setCellValueFactory(new PropertyValueFactory<>("Serpentine_run"));
Score4.setCellValueFactory(new PropertyValueFactory<>("Score4"));
TotalScore.setCellValueFactory(new PropertyValueFactory<>("TotalScore"));
(2)元の挿入方式
setCellValueFactoryメソッドを書き換え、Integerの場合、double汎用統合はNumberを使用し、paramは現在のモデルクラスの指向であることに注意します.
Ranking.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Scores, Number>, ObservableValue<Number>>() {
@Override
public ObservableValue<Number> call(TableColumn.CellDataFeatures<Scores, Number> param) {
Collections.sort(Ranking_data, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1>= o2) {
return -1;
}
else {
return 1;
}
}
});
System.out.println(Ranking_data);
int Ranks = Ranking_data.indexOf(param.getValue().getTotalScore())+1;
SimpleIntegerProperty ranking = new SimpleIntegerProperty(Ranks);
return ranking;
}
});
4.入力データ
ObservableList<Scores> data = FXCollections
.observableArrayList(scores);
ranking_table.setItems(data);