我想讓表格單元格顯示字符串,當我創建新的行。但所有的行都只是空的。有誰知道我做錯了什麼? 這裏是主要類: 包應用程序;表格單元在我的tableview中是空的。 JavaFX + Scenebuilder
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/fxml/BasketCase_GUI_0.3.fxml"));
Scene scene = new Scene(root,1110,740);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.setTitle("Basket Case_Beta");
primaryStage.show();
scene.setCursor(Cursor.DEFAULT);
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
這是正常的,所以我不認爲你需要擔心那個。
這裏是控制器類。我認爲問題可能在哪裏。
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class MainController implements Initializable {
@FXML
TableView<Table> TableID;
@FXML
TableColumn<Table, Integer> aPlayerID;
@FXML
TableColumn<Table, String> aLeague;
@FXML
TableColumn<Table, String> aName;
private int aNumber = 1;
SimpleStringProperty str = new SimpleStringProperty();
public MainController() {
str.set("Hello");
}
final ObservableList<Table> data = FXCollections.observableArrayList(
new Table(aNumber++, "hehe", "hoho"),
new Table(aNumber++, "hehe", "hoho"),
new Table(aNumber++, "hehe", "hoho")
);
public void buttonClick(ActionEvent event) {
data.add(new Table(aNumber++, "hehe", "hoho"));
TableID.getColumns().addAll(aPlayerID, aLeague, aName);
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
aPlayerID.setCellValueFactory(new PropertyValueFactory<Table, Integer>("bPlayerID"));
aLeague.setCellValueFactory(new PropertyValueFactory<Table, String>("bLeague"));
aName.setCellValueFactory(new PropertyValueFactory<Table, String>("bName"));
TableID.setItems(data);
}
}
而且這裏也是所需的TableViewer
package application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class Table {
private final SimpleIntegerProperty bPlayerID;
private final SimpleStringProperty bLeague;
private final SimpleStringProperty bName;
public Table(int cPlayerID, String cLeague, String cName) {
this.bPlayerID = new SimpleIntegerProperty(cPlayerID);
this.bLeague = new SimpleStringProperty(cLeague);
this.bName = new SimpleStringProperty(cName);
}
public int getbPlayerID() {
return bPlayerID.get();
}
public void setbPlayerID(int v) {
bPlayerID.set(v);
}
public String getbLeague() {
return bLeague.get();
}
public void setbLeague(String v) {
bLeague.set(v);
}
public String getbName() {
return bName.get();
}
public void setbName(String v) {
bName.set(v);
}
}
表類多數民衆贊成。你們知道什麼可能是錯誤或者可能建議我可以如何與代碼仍然作品增添剛剛的TableViewer場景構建器中的其他fxml文件?
謝謝! 但是,這隻適用於intergerproperty。我嘗試過StringProperty,但由於某種原因它不工作。字符串單元格仍爲空 – Tilion 2014-11-22 17:49:14
您對其他方法做了相同的更改嗎? – 2014-11-22 17:50:21
是的,它現在看起來是這樣的: \t **我只注意到我不能在註釋中顯示的代碼** \t公共StringProperty getbLeague(){ \t \t回報bLeague; \t} – Tilion 2014-11-22 17:51:43