我的表格視圖中的表格單元為空。JavaFX + Scenebuilder
您的get方法名稱錯誤。根據PropertyValueFactory文檔,如果傳入屬性名稱“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方法的情況下,它不會更新屬性。
最好使用“ 屬性和綁定”教程中的大綱定義表模型:
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());
這將允許編譯器捕獲任何錯誤,而不僅僅是在運行時靜默失敗。
解決方法我試圖讓表單元格在創建新行時顯示字符串。但是所有行都是空的。有人知道我在做什么錯嗎?這是主要的類:包應用程序;
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++,'hoho') ); public void buttonClick(ActionEvent event) {data.add(new Table(aNumber++,'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,使其代碼仍可與SceneBuilder中的其余fxml文件一起使用?
相關文章:
1. python的文件讀寫問題?2. javascript - jquery hide()方法無效3. mysql里的大表用mycat做水平拆分,是不是要先手動分好,再配置mycat4. 怎么用css截取字符?5. css - 定位為absolute的父元素中的子元素 如何設置在父元素的下面?6. python - 獲取到的數據生成新的mysql表7. CSS3可否做出這個效果?8. window下mysql中文亂碼怎么解決??9. javascript - 圖片鏈接請求一直是pending狀態,導致頁面崩潰,怎么解決?10. javascript - 請問 chrome 為什么會重復加載圖片資源?
