2016-10-29 7 views
1

私はチュートリアルを見てきましたが、テーブルを作成できないようです。 私はネットビーンとシーンビルダも使用しています。 ご協力いただければ幸いです! 5時間苦労しました。ここでJavaFXテーブルの列、SceneBuilderにデータが入っていない

Controllerクラスの私のコードです:ここでは

public class FXMLDocumentController implements Initializable { 

    @FXML 
    private TableView<Table> table; 
    @FXML 
    private TableColumn<Table, String> countriesTab; 

    /** 
    * Initializes the controller class. 
    */ 

    ObservableList<Table> data = FXCollections.observableArrayList(
      new Table("Canada"), 
      new Table("U.S.A"), 
      new Table("Mexico") 
    ); 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 

     countriesTab.setCellValueFactory(new PropertyValueFactory<Table, String>("rCountry")); 
     table.setItems(data); 
    } 
} 

はここTable

class Table { 
    public final SimpleStringProperty rCountry; 


    Table(String country){ 
     this.rCountry = new SimpleStringProperty(country); 
    } 

    private SimpleStringProperty getRCountry(){ 
     return this.rCountry; 

    } 
} 

ための私のコードで私のメインされていますPropertyValueFactoryについては

public class Assignment1 extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+0

あなたはFXMLコード –

答えて

2

見つけるために、プロパティitemクラス(この場合はTable)neパッケージのプライベートではなく、アクセス修飾子としてeds publicを使用します。プロパティを返すメソッドはpublicである必要があります。

さらに、PropertyValueFactoryが機能するために必要な規則に従って、プロパティ自体を返すメソッドの正しい名前は<nameOfProperty>Propertyです。

また、プロパティの実際の型は実装の詳細であることから、あなたがへの書き込みアクセスを防止するために、これらの修飾子を使用する場合には、戻り値の型の代わりに、SimpleStringProperty

public class Table { 

    private final SimpleStringProperty rCountry; 

    public Table(String country){ 
     this.rCountry = new SimpleStringProperty(country); 
    } 

    public StringProperty rCountryProperty() { 
     return this.rCountry; 
    } 
} 

としてStringPropertyを使用する方がデザインだろうプロパティは、あなたはまだReadOnlyStringWrapperを使用してこの効果を実現して返すことができReadOnlyStringProperty:場合

public class Table { 

    private final ReadOnlyStringWrapper rCountry; 

    public Table(String country){ 
     this.rCountry = new ReadOnlyStringWrapper (country); 
    } 

    public ReadOnlyStringProperty rCountryProperty() { 
     return this.rCountry.getReadOnlyProperty(); 
    } 
} 

がすべてで、単に財産への書き込みアクセスはありませんプロパティのゲッターを使用するだけで十分です。あなたは、このケースではまったくStringPropertyを使用する必要はありません。

public class Table { 

    private final String rCountry; 

    public Table(String country){ 
     this.rCountry = country; 
    } 

    public String getRCountry() { 
     return this.rCountry; 
    } 
} 
+0

を投稿してくださいすることができ、あなたの助けをありがとう!私はかなり長い間このことに苦労してきました。私はそれを今ソートしている:) – Evan

関連する問題