Javafx更新对象变化的tableview
我在更新我的java类中的tableview时遇到了一些麻烦。Javafx更新对象变化的tableview
package gameStats.controllers;
import gameStats.Main;
import gameStats.model.Team;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
public class MainController implements Initializable {
private Timeline timeline = new Timeline();
@FXML
private Label timerText;
@FXML
private Button startTimerButton, stopTimerButton, resetTimerButton, addTeamButton, addPointButton, removePointButton, newTimeButton;
@FXML
private TextField teamNameTextfield;
@FXML
private TableView teamView;
@FXML
private TableColumn<Team, SimpleStringProperty> nameCol;
@FXML
private TableColumn<Team, SimpleIntegerProperty> pointCol;
private ObservableList<Team> obsTeamList;
private int min;
private int startTimeSec, startTimeMin;
private Parent borderPane;
public BorderPane timeBorderPane;
private boolean isRunning;
public void startTimer() {
if(isRunning == false) {
if (!(startTimeMin < 0)) {
KeyFrame keyframe = new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
startTimeSec--;
boolean isSecondsZero = startTimeSec == 0;
boolean timeToChangeBackground = startTimeSec == 0 && startTimeMin == 0;
if (isSecondsZero) {
startTimeMin--;
startTimeSec = 60;
}
if (timeToChangeBackground) {
timeline.stop();
startTimeMin = 0;
startTimeSec = 0;
timerText.setTextFill(Color.RED);
}
timerText.setText(String.format("%d min, %02d sec", startTimeMin, startTimeSec));
}
});
timerText.setTextFill(Color.BLACK);
startTimeSec = 60; // Change to 60!
startTimeMin = min - 1;
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.getKeyFrames().add(keyframe);
timeline.playFromStart();
isRunning = true;
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "You have not entered a time!");
alert.showAndWait();
}
}else {
timeline.play();
}
}
public void setTimer() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("newTimeDialog.fxml"));
Parent newTimeBorderPane = (BorderPane) loader.load();
borderPane = newTimeBorderPane;
Scene scene = new Scene(newTimeBorderPane);
Stage primaryStage = new Stage();
primaryStage.setScene(scene);
primaryStage.showAndWait();
if (!primaryStage.isShowing()) {
min = NewTimeController.getMin();
timerText.setText(min + "min, " + 00 + "sec");
} else {
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void stopTimer() {
timeline.pause();
}
public void resetTimer() {
timeline.stop();
startTimeSec = 60;
startTimeMin = min-1;
timerText.setText(String.format("%d min, %02d sec", startTimeMin, startTimeSec));
}
public void createTeam(){
SimpleStringProperty name = new SimpleStringProperty(teamNameTextfield.getText());
SimpleIntegerProperty startPoint = new SimpleIntegerProperty(0);
if(!(obsTeamList.size() == 0)){
for (Team t : obsTeamList) {
if (!t.getName().equals(name)) {
obsTeamList.add(new Team(name, startPoint));
teamView.getItems().setAll(obsTeamList);
}else {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Holdet eksistere allerede!");
alert.showAndWait();
}
}
}else{
obsTeamList.add(new Team(name, startPoint));
teamView.getItems().setAll(obsTeamList);
}
}
public void addPoint(){
Team teamToAddPointsTo = (Team) teamView.getSelectionModel().getSelectedItem();
teamToAddPointsTo.setPoints(new SimpleIntegerProperty(1));
}
@Override
public void initialize(URL location, ResourceBundle resources) {
obsTeamList = FXCollections.observableArrayList(new ArrayList<>());
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
pointCol.setCellValueFactory(new PropertyValueFactory<>("points"));
}
}
模型类:
package gameStats.model;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
* Created by lassebjorklund on 21/01/16.
* For JuniorEvent_kampdata
*/
public class Team {
private SimpleStringProperty name;
private SimpleIntegerProperty points;
public Team(SimpleStringProperty name, SimpleIntegerProperty points) {
this.name = name;
this.points = points;
}
public String getName() {
return name.get();
}
public SimpleStringProperty nameProperty() {
return this.name;
}
public int getPoints() {
return points.get();
}
public SimpleIntegerProperty pointsProperty() {
return this.points;
}
public void setName(String name) {
this.name.set(name);
}
public void setPoints(SimpleIntegerProperty points) {
this.points.add(points);
}
@Override
public String toString() {
return getName() + " : " + getPoints();
}
}
我希望能在表视图中选择团队更新点。当我按下一个调用“addPoint”方法的按钮时,我希望发生这种情况。但我不知道该怎么做。
您的代码有多个问题。
- 列的类型应该是基础数据类型,而不是属性类型。对于
SimpleStringProperty
使用TableColumn<Team, String>
。对于SimpleIntegerProperty
使用TableColumn<Team, Integer>
或TableColumn<Team, Number>
。 -
setPoints
方法应采取int
或Integer
而不是SimpleIntegerProperty
。它应该是getPoints
返回的相同类型。 - Inside
setPoints
,您应该使用points.set
。points.add
创建了一个新的NumberExpression
,表示添加了这两个属性,但不更改实际属性。
在相同的音符,它可能是有Team
作为最终场的实际性能,只有改变它们的值是一个好主意。有关更多信息,请参阅this tutorial。
谢谢,这解决了我的问题。 – Lasse
[JavaFX 8教程链接版本](http://docs.oracle.com/javase/8/javafx/properties-binding-tutorial/binding.htm#JFXBD107)(尽管我认为它可能与文章完全相同)和(更有帮助)[有用的相关PowerPoint演示文稿](http://www.oracle.com/us/technologies/java/javafx-2-prog-model-1524061.pdf)。 –
嗨@Lasse我建议你将标题改为更具描述性的内容, – Llewellyn1411
完成,谢谢@ Llewellyn1411 – Lasse
没有你告诉我们你想如何更新视图(结果应该是什么以及何时应该发生),这个将很难弄清楚。 – hotzst