2016-10-29 2 views
1

자습서를 살펴 봤는데 테이블을 채울 수없는 것 같습니다. net beans와 scenebuilder도 사용하고 있습니다. 도움이 될 것입니다. 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

당신은 –

답변

2

것은 찾을 수 속성 항목 클래스 (즉 Table) ne eds public을 액세스 한정자로 사용하고 개인 패키지는 사용하지 마십시오. 속성을 반환하는 메서드는 public이어야합니다.

또한 PropertyValueFactory이 작동하는 데 필요한 규칙에 따라 속성 자체를 반환하는 메서드의 올바른 이름은 <nameOfProperty>Property입니다. 재산의 실제 유형이 구현 세부 사항이기 때문에

또한, 당신이 쓰기 액세스를 방지하기 위해이 수정을 사용하는 경우

public class Table { 

    private final SimpleStringProperty rCountry; 

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

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

을 반환 형식으로 StringProperty를 사용하는 대신 SimpleStringProperty 더 좋은 디자인이 될 것입니다 속성은, 당신은 여전히 ​​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(); 
    } 
} 

전혀 단순히 재산에 더 쓰기 권한이 없습니다 속성에 대해 getter를 사용하면 충분합니다. 이 경우 전혀 StringProperty를 사용할 필요가 없습니다 :

public class Table { 

    private final String rCountry; 

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

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

여러분의 도움에 감사드립니다 당신의 fxml 코드를 게시하시기 바랍니다 수 있습니다! 나는 이것을 꽤 오랫동안 고심 해왔다. 나는 그것을 지금 분류했다 :) – Evan

관련 문제