javafx button.onclick()无效

时间:2016-05-12 01:42:18

标签: javafx

当我点击它不起作用的按钮时,我遇到了问题。 实际上,我只是要求按钮执行system.out.println()只是为了测试。按钮的名称是btndetailUser

    listRechercheUser.setCellFactory(new Callback<ListView<User>, ListCell<User>>() {
        @Override
        public ListCell<User> call(ListView<User> p) {
            ListCell<User> cell;
            cell = new ListCell<User>() {

                @Override
                protected void updateItem(User user, boolean bln) {
                    super.updateItem(user, bln);
                    if (user != null) {
                        ImageView img;
                        String mm=user.getPath().trim();
                        if (mm.equals("fusee.png")){
                        img = new ImageView(new Image("mto/cr/images/"+mm));}
                        else{
                        img = new ImageView(new Image("mto/cr/images/mains-fonds.png"));

                        }
                        VBox vb = new VBox(10);
                        HBox hb = new HBox(20);
                        VBox vb1 = new VBox(20);
                        VBox vb3= new VBox(20);
                        vb1.getChildren().add(new Label("               "));
                        hb.setStyle("-fx-border-color: #C8C8C8 ;");
                        vb.setAlignment(Pos.CENTER);
                        VBox vb2 = new VBox(30);
                        vb.setAlignment(Pos.CENTER);
                        vb2.setAlignment(Pos.BASELINE_LEFT);
                        vb3.setAlignment(Pos.CENTER_RIGHT);
                        Label id = new Label(user.getId()+"id firas:");
                        Label username = new Label(user.getUsername()+" username firas:");
                        Label nom = new Label(user.getNom()+"nom firas:");
                        Label prenom = new Label(user.getPrenom()+"prenom firas:");
                       Button btndetailUser = new Button();
                        btndetailUser.setId("btndetailUser");
                         btndetailUser.setText("btndetailUser "+user.getId());
                         Image imageAccesProjet = new Image("mto/cr/images/enter.png");
                        ImageView enterIV= new ImageView(imageAccesProjet); 
                        enterIV.setFitHeight(10);
                        enterIV.setFitWidth(10);
                            btndetailUser.setGraphic(enterIV);
                           btndetailUser.getStyleClass().add("buttonLogin");

                        System.out.println(btndetailUser.getText());
                           btndetailUser.setOnMouseClicked(
                                   (MouseEvent event2) ->
                                                              System.out.println("firas"));
                           btndetailUser.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                                System.out.println(".handle()");
                            }
                        });


                       vb.getChildren().addAll(new Label("  "), img, new Label(" "),username);
                        vb2.getChildren().addAll(nom, prenom);
                        vb3.getChildren().addAll(btndetailUser);
                        hb.getChildren().addAll(vb1, vb, vb2,vb3);

                        setGraphic(hb);
                    }
                }
            };
            return cell;
        }
    });

1 个答案:

答案 0 :(得分:1)

尝试类似的事情,

btndetailUser.setOnAction(e -> {
    System.out.println("freetext")
});