表格单元在我的tableview中是空的。 JavaFX + Scenebuilder
我想让表格单元格显示字符串,当我创建新的行。但所有的行都只是空的。有谁知道我做错了什么? 这里是主要类: 包应用程序;表格单元在我的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文件?
您的get
方法的名称是错误的。根据PropertyValueFactory
documentation,如果您传入属性名称“xyz”,属性值工厂将首先查找属于表格行中对象的方法xyzProperty()
。如果没有找到,它会回退寻找一种叫做getXyz()
的方法(仔细看看那里的大写字母),将结果包装在ReadOnlyObjectWrapper
中。
所以下面将工作:
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);
}
}
然而,随着PropertyValueFactory
文件中指出,在这种情况下,性能不会是“活”:换句话说,如果该值的变化,该表将不会自动更新。另外,如果你想让表格可编辑,它不会更新属性,没有一些明确的接线来调用set方法。
这是更好地使用大纲在Properties and Bindings tutorial来定义你的表格模型:
package application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Table {
private final IntegerProperty bPlayerID;
private final StringProperty bLeague;
private final StringProperty 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 IntegerProperty bPlayerIDProperty() {
return bPlayerID ;
}
public String getBLeague() {
return bLeague.get();
}
public void setBLeague(String v) {
bLeague.set(v);
}
public StringProperty bLeagueProperty() {
return bLeague ;
}
public String getBName() {
return bName.get();
}
public void setBName(String v) {
bName.set(v);
}
public StringProperty bNameProperty() {
return bName ;
}
}
如果你这样做,那么(在Java中8),你可以使用下面的单元格值的工厂,而不是PropertyValueFactory
:
aPlayerID.setCellValueFactory(cellData -> cellData.getValue().bPlayerIDProperty());
这将允许编译器捕获任何错误,而不是只是在运行时静静地失败。
在stringproperty你gotto cellData.getValue()。bPlayerIDProperty之后添加asobjects()方法())......所以我希望这将有助于
谢谢! 但是,这只适用于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