JavaFX的列表视图,在每个单元格按钮 [英] javafx listview with button in each cell

查看:123
本文介绍了JavaFX的列表视图,在每个单元格按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含有自定义单元格工厂列表视图JavaFX应用程序。每个单元包含一个按钮做该小区特定操作。升级到Java 8之前,每一件事情是确定的,但运行我的应用程序与Java 8时,我按一下按钮,而不是通过按钮操作事件之后,选择了全细胞。这里是我的code:

 进口javafx.event.ActionEvent;
进口javafx.event.EventHandler;
进口javafx.scene.control.Button;
进口javafx.scene.control.Label;
进口javafx.scene.control.ListCell;
进口javafx.scene.input.MouseEvent;
进口javafx.scene.layout.GridPane;公共类CustomCell扩展的ListCell<串GT; {
    私人按钮actionBtn;    公共CustomCell(){
        超();        setOnMouseClicked(新的EventHandler<&的MouseEvent GT;(){            @覆盖
            公共无效手柄(事件的MouseEvent){
                //做一点事
            }
        });
    }    @覆盖
    公共无效的updateItem(字符串项,布尔空){
        super.updateItem(项目,空);
        setEditable(假);
        如果(项目!= NULL){
            标签名称=新标签(项目);
            actionBtn =新按钮(我的行动);
            actionBtn.setOnAction(新的EventHandler<&ActionEvent的GT;(){                @覆盖
                公共无效手柄(ActionEvent的为arg0){
                    的System.out.println(hiiiiiiiii);                }
            });            GridPane窗格=新GridPane();
            。pane.getStyleClass()加(gridpane);
            pane.add(姓名,0,0);
            pane.add(actionBtn,0,1);
            setGraphic(面板);
        }其他{
            的setText(NULL);
            setGraphic(NULL);
        }
    }
}


解决方案

这是一个错误,对已经立案。请参见 OTN上这次讨论。

由于在讨论中,我会强烈建议不建立在的updateItem(...)法新控件。这是更好一次创建它们的单元格,然后配置它们在的updateItem(...)。例如:

 进口javafx.event.ActionEvent;
进口javafx.event.EventHandler;
进口javafx.scene.control.Button;
进口javafx.scene.control.Label;
进口javafx.scene.control.ListCell;
进口javafx.scene.input.MouseEvent;
进口javafx.scene.layout.GridPane;公共类CustomCell扩展的ListCell<串GT; {
    私人按钮actionBtn;
    私人标签名称;
    私人GridPane窗格;    公共CustomCell(){
        超();        setOnMouseClicked(新的EventHandler<&的MouseEvent GT;(){            @覆盖
            公共无效手柄(事件的MouseEvent){
                //做一点事
            }
        });        actionBtn =新按钮(我的行动);
        actionBtn.setOnAction(新的EventHandler<&ActionEvent的GT;(){
            @覆盖
            公共无效手柄(ActionEvent的事件){
                的System.out.println(行动:+的getItem());
            }
        });
        名称=新标签();
        窗格=新GridPane();
        pane.add(姓名,0,0);
        pane.add(actionBtn,0,1);
        的setText(NULL);
    }    @覆盖
    公共无效的updateItem(字符串项,布尔空){
        super.updateItem(项目,空);
        setEditable(假);
        如果(项目!= NULL){
            name.setText(项目);
            setGraphic(面板);
        }其他{
            setGraphic(NULL);
        }
    }
}

除了作为有效的多,这也提供了错误一种变通方法。

I have a javafx application which contains a listview with a custom cell factory. each cell contains a button to do a specific action for that cell. before updating to java 8 every thing was ok but after running my app with java 8 when i click the button instead of handling event by button, the whole cell is selected. Here is my code:

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;

public class CustomCell extends ListCell<String> {
    private Button actionBtn;   

    public CustomCell() {
        super();    

        setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {              
                //do something                  
            }
        });     
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        setEditable(false);
        if (item != null) {
            Label name = new Label(item);                           
            actionBtn = new Button("my action");            
            actionBtn.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent arg0) {
                    System.out.println("hiiiiiiiii");

                }
            });                     

            GridPane pane = new GridPane();
            pane.getStyleClass().add("gridpane");           
            pane.add(name, 0, 0);
            pane.add(actionBtn, 0, 1);              
            setGraphic(pane);
        } else {
            setText(null);
            setGraphic(null);
        }
    }   
}

解决方案

That's a bug, which has been filed. See this discussion on OTN.

As in that discussion, I would strongly recommend not creating new controls in the updateItem(...) method. It's much better to create them once for the cell, and then to configure them in updateItem(...). For example:

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;

public class CustomCell extends ListCell<String> {
    private Button actionBtn;   
    private Label name ;
    private GridPane pane ;

    public CustomCell() {
        super();    

        setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {              
                //do something                  
            }
        }); 

        actionBtn = new Button("my action");
        actionBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Action: "+getItem());
            }
        });
        name = new Label();
        pane = new GridPane();
        pane.add(name, 0, 0);
        pane.add(actionBtn, 0, 1);
        setText(null);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        setEditable(false);
        if (item != null) {
            name.setText(item);                          
            setGraphic(pane);
        } else {
            setGraphic(null);
        }
    }   
}

As well as being considerably more efficient, this also provides a workaround for the bug.

这篇关于JavaFX的列表视图,在每个单元格按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆