2

我需要一些帮助,即使我的情况很简单。但我不明白为什么表格视图中的复选框没有获得价值。我从 javafx ensemble 得到了这个例子

我有一堂课

public class ReservationObj {
    private  BooleanProperty        tcheck;        
    private  StringProperty         tname;      
    private  StringProperty         tstatus;       
    private  int                    tser;

    public ReservationObj(boolean tcheck, String lname , String lBStatus, int serialinVector) {

        this.tcheck = new SimpleBooleanProperty(tcheck);            
        this.tname = new SimpleStringProperty(lname); 
        this.tstatus = new SimpleStringProperty(lBStatus);  

        this.tser = serialinVector;   

        this.tcheck.addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
                System.out.println("The check Box is: " + t1);
            }
        });

    }

    public BooleanProperty getTcheck() {return tcheck;}

    public String   getTname() {return tname.getValue() ;}

    public void setTname(String tname) {this.tname.set(tname);}

    public String  getTstatus() {return tstatus.getValue() ;}

    public void setTstatus(String tstatus) {this.tstatus.set(tstatus);}

    public int getTser() {return tser;}

    public void setTser(int tser) { this.tser = tser;}

}   

我也有。

public Parent createContent() {

final  ObservableList<ReservationObj> ReservationList = FXCollections.observableArrayList(
        new ReservationObj(true, "aaaaaaaa", "bbbbbbbbb", 1));

    TableColumn RCheckCol = new TableColumn<ReservationObj, Boolean>();

    RCheckCol.setCellValueFactory(new PropertyValueFactory("tcheck"));          
    RCheckCol.setText("aaa");        
    RCheckCol.setCellFactory(new Callback<TableColumn<ReservationObj, Boolean>, TableCell<ReservationObj, Boolean>>() {

        public TableCell<ReservationObj, Boolean> call(TableColumn<ReservationObj, Boolean> p) {

            return new CheckBoxTableCell<ReservationObj, Boolean>();

        }
    });

    TableColumn RNameCol = new TableColumn();
    RNameCol.setCellValueFactory(new PropertyValueFactory("tname"));        
    RNameCol.setText("bbbb");

    TableColumn RAgeCol = new TableColumn();
    RAgeCol.setCellValueFactory(new PropertyValueFactory("tstatus"));
    RAgeCol.setText("cccc");

    TableView AAView   = new TableView();           

    AAView.setItems(ReservationList);          
    AAView.setEditable(true);        
    AAView.getColumns().addAll(RCheckCol,RNameCol,RAgeCol);      

    return AAView;

}

而当简单。

@Override
public void start(Stage primaryStage) {
    primaryStage.setScene(new Scene(createContent()));
    primaryStage.show();

}

表格视图的列正在获取除第一个“复选框”之外的值。复选框中的侦听器也不起作用。我真的不明白我做错了什么。因为我从合奏中得到了这个例子。感谢您的每一个想法和任何解决方案。埃利亚斯

4

1 回答 1

6

您的类 在命名方法ReservationObj中不遵守JavaFX 属性约定。

如果您想将inRCheckColtcheck BooleanPropertyin 绑定:

  RCheckCol.setCellValueFactory(new PropertyValueFactory("tcheck")); 

tcheckProperty您必须在模型类中提供一个方法:

public BooleanProperty tcheckProperty() {
        return tcheck;
    }

作为一个有效的例子JavaFX Bean

public class Person {

    private StringProperty name = new SimpleStringProperty("");

    public Person(String name) {
        this.name.setValue(name);
    }

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

    public void setName(String name) {
        this.name.setValue(name);
    }

    public StringProperty nameProperty() {
        return name;
    }
}
于 2013-08-27T10:37:27.977 回答