2013-08-27 2 views
2

J'ai besoin d'aide, même mon cas est simple. Mais je n'arrive pas à comprendre pourquoi la case à cocher de la vue de table n'obtient pas la valeur. j'obtenir l'exemple de l'ensemble de JavaFXjavafx 2 CheckBoxTableCell ne fonctionne pas dans tableview

J'ai une classe

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;} 

} 

Et aussi j'ai le.

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; 

} 

Et quand c'est simple.

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

} 

Les colonnes de la vue de table obtiennent les valeurs à l'exception de la première case à cocher. L'écouteur dans la case à cocher ne fonctionne pas non plus. Je ne comprends vraiment pas ce que j'ai fait de mal. Parce que j'ai l'exemple de l'ensemble. Merci pour toutes les idées et toutes les solutions. Elias

+0

voir cet exemple: - http://stackoverflow.com/questions/18374626/javafx-2-tableview-return-selected-item/18382098#18382098 –

Répondre

6

Votre classe ReservationObj ne respecte pas la convention JavaFX Properties dans les méthodes de nommage.

Si vous voulez lier le RCheckCol avec le tcheckBooleanProperty dans:

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

Vous devez fournir une méthode tcheckProperty dans votre classe de modèle:

public BooleanProperty tcheckProperty() { 
     return tcheck; 
    } 

À titre d'exemple un JavaFX Bean valide:

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; 
    } 
} 
+0

Merci beaucoup pour votre réponse. Oui, vous avez raison, cela fonctionne. Merci encore. –

+0

Cela a résolu un bug de longue date dans mon application. Merci! –