2015-01-08 3 views
1

많은 링크를 따라 가면서 테이블보기에 확인란을 표시하는 솔루션을 발견했습니다. 그러나 테이블보기에서 확인란의 값을 변경할 수 없습니다.JavaFX TableView의 속성 바인딩이 작동하지 않습니다.

링크는 내가 다음 : How to add CheckBox's to a TableView in JavaFX

모델 클래스 :

public class TUser { 
    private SimpleStringProperty name; 
    private SimpleStringProperty address; 
    private SimpleBooleanProperty active; 

public TUser(String name, String address, boolean active) { 
    this.name = new SimpleStringProperty(name); 
    this.address = new SimpleStringProperty(address); 
    this.active = new SimpleBooleanProperty(active); 
} 

public String getName() { 
    return name.get(); 
} 

public void setName(String name) { 
    this.name = new SimpleStringProperty(name); 
} 

public String getAddress() { 
    return address.get(); 
} 

public void setAddress(String address) { 
    this.address = new SimpleStringProperty(address); 
} 

public boolean getActive() { 
    return active.get(); 
} 

public void setActive(boolean active) { 
    this.active = new SimpleBooleanProperty(active); 
} 

} 

FXML 파일 :

<TableView fx:id="usertable" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> 
    <columns> 
     <TableColumn fx:id="name" prefWidth="142.0" text="Name" /> 
    <TableColumn fx:id="address" prefWidth="147.0" text="Address" /> 
     <TableColumn fx:id="active" prefWidth="153.0" text="Active" /> 
    </columns> 
    <columnResizePolicy> 
     <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> 
    </columnResizePolicy> 
</TableView> 

컨트롤러 클래스에게 :

public class UserManagement extends AnchorPane { 

    @FXML 
    private TableView<TUser> usertable; 
    @FXML 
    private TableColumn<TUser, String> address; 
    @FXML 
    private TableColumn<TUser, String> name; 
    @FXML 
    private TableColumn<TUser, Boolean> active; 

    public UserManagement() throws IOException { 
     initGraphics(); 
     initTable(); 
    } 

    private class CheckBoxCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> { 
    @Override public TableCell<S, T> call(TableColumn<S, T> p) { 
     return new CheckBoxTableCell<>(); 
    } 
} 

    private void initGraphics() throws IOException { 
     FXMLLoader content = new FXMLLoader(getClass().getResource("/mypack/fxmls/UserManagement.fxml")); 
     content.setController(this); 
     Node contentnode = (Node) content.load(); 
     AnchorPane.setBottomAnchor(contentnode, 0.0); 
     AnchorPane.setLeftAnchor(contentnode, 0.0); 
     AnchorPane.setRightAnchor(contentnode, 0.0); 
     AnchorPane.setTopAnchor(contentnode, 0.0); 
     getChildren().add(contentnode); 

     active.setCellFactory(new CheckBoxCellFactory<TUser, Boolean>()); 

     address.setCellValueFactory(new PropertyValueFactory<TUser, String>("address")); 
     name.setCellValueFactory(new PropertyValueFactory<TUser, String>("name")); 
     active.setCellValueFactory(new PropertyValueFactory<TUser, Boolean>("active")); 

    } 

    private void initTable(){ 
     ObservableList<TUser> data = FXCollections.observableArrayList(new TUser("ABC", "ABC Road", true)); 
     usertable.setItems(data); 
    } 

} 

출력 :

public class TUser { 

    // other methods and fields as before.... 

    public BooleanProperty activeProperty() { 
     return active ; 
    } 

} 

그것은 정의하는 것이 좋습니다는 아마 :

TableView

답변

1

확인란이 제대로 모델 클래스의 속성에 바인딩하려면, 당신은 속성 접근 방법을 정의 할 필요가 다른 속성들에 대해서도 비슷한 방법.

테이블을 편집 할 수 없으므로 확인란을 사용자가 조작 할 수 없습니다. 당신이 그 (것)들을 편집 할 수 원하는 경우, TableView 편집합니다

<TableView fx:id="usertable" editable="true" ...> 

TableColumn의는 기본적으로 편집 할 수 있습니다.

+0

하지만 편집 가능하게하고 싶지 않습니다. 사용자가 편집 할 수 없도록 데이터 표시 만하고 싶습니다. –

+0

아, 좋아. ("나는 체크 박스의 값을 바꿀 수 없다"사용자가 그들을 변경할 수 있기를 바랬다. 실수였다.) 그냥 속성 접근 자 메서드를 정의하면 속성에 제대로 바인딩된다. –

+0

그에 따라 답변이 업데이트되었습니다. –

관련 문제