2016-01-22 53 views
1

我在更新我的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」方法的按鈕時,我希望發生這種情況。但我不知道該怎麼做。

+0

嗨@Lasse我建議你將標題改爲更具描述性的內容, – Llewellyn1411

+1

完成,謝謝@ Llewellyn1411 – Lasse

+1

沒有你告訴我們你想如何更新視圖(結果應該是什麼以及何時應該發生),這個將很難弄清楚。 – hotzst

回答

3

您的代碼有多個問題。

  1. 列的類型應該是基礎數據類型,而不是屬性類型。對於SimpleStringProperty使用TableColumn<Team, String>。對於SimpleIntegerProperty使用TableColumn<Team, Integer>TableColumn<Team, Number>
  2. setPoints方法應採取intInteger而不是SimpleIntegerProperty。它應該是getPoints返回的相同類型。
  3. Inside setPoints,您應該使用points.setpoints.add創建了一個新的NumberExpression,表示添加了這兩個屬性,但不更改實際屬性。

在相同的音符,它可能是有Team作爲最終場的實際性能,只有改變它們的值是一個好主意。有關更多信息,請參閱this tutorial

+0

謝謝,這解決了我的問題。 – Lasse

+1

[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)。 –