Javafx在对象更改时更新tableview
我在更新java类中的tableview时遇到一些问题Javafx在对象更改时更新tableview,java,uitableview,javafx,tableview,observablecollection,Java,Uitableview,Javafx,Tableview,Observablecollection,我在更新java类中的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.prop
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"));
}
}
我希望能够在表视图中更新所选团队的点。我希望在按下调用“addPoint”方法的按钮时发生这种情况。但我不知道怎么做 您的代码有多个问题
SimpleStringProperty
使用TableColumn
。对于SimpleIntegerProperty
使用TableColumn
或TableColumn
李>
设置点
方法应采用int
或整数
,而不是SimpleIntegerProperty
。它应该与getPoints
返回的类型相同李>
设置点
内,应使用点。设置
points.add
创建一个新的NumberExpression
,表示两个属性的添加,但不更改实际属性李>
同样,最好将
Team
的实际属性作为最终字段,只更改它们的值。有关更多信息,请参阅 您好@Lasse我建议您将标题更改为更具描述性、完成并感谢@Llewellyn1411,而不告诉我们您希望如何更新视图(结果应该是什么以及何时发生),这将很难理解。(尽管我认为可能是同一篇文章)和(更有帮助的)一篇文章。
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();
}
}