java 如何在javafx中向tableview单元格添加点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36950130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:02:18  来源:igfitidea点击:

How to add a click event to a tableview cell in javafx

javauser-interfacejavafxtableviewscenebuilder

提问by Jonathan

My goal is to detect when a user double clicks a cell in the TableViewand use the information from that cell. From my picture you can see I will have a table of beers, breweries, and style.

我的目标是检测用户何时双击 中的单元格TableView并使用该单元格中的信息。从我的照片中你可以看到我将有一张啤酒、啤酒厂和风格的桌子。

Upon double clicking a cell I want to show the user an image (of the beer, a brewery) with some info. I am using scene builder as well, so I am dealing with controller classes. So far what I have is this but no luck. No errors, just doesn't pull the info when I attempt a basic test.

双击一个单元格后,我想向用户显示带有一些信息的图像(啤酒、啤酒厂)。我也在使用场景构建器,所以我正在处理控制器类。到目前为止,我所拥有的是这个,但没有运气。没有错误,只是在我尝试基本测试时不提取信息。

FYI: I want to detect the click on one cell, and ONLY pull info from the cell clicked - not the entire row.

仅供参考:我想检测对一个单元格的点击,并且只从点击的单元格中提取信息 - 而不是整行。

screenshot of table with beers and breweries

带有啤酒和啤酒厂的桌子的屏幕截图

Here is my code for the event.

这是我的活动代码。

public void clickItem(MouseEvent event) {
    tableID.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            System.out.println("Clicked on " + (tableID.getSelectionModel().getSelectedCells().get(0)).getColumn());        
        }
    });
}

回答by Rana Depto

If you are using scene builder & controller class, then why you are using setOnMouseClicked on that method? Instead of that try this:

如果您使用的是场景构建器和控制器类,那么为什么要在该方法上使用 setOnMouseClicked?而不是尝试这个:

@FXML
public void clickItem(MouseEvent event)
{
    if (event.getClickCount() == 2) //Checking double click
    {
        System.out.println(tableID.getSelectionModel().getSelectedItem().getBeer());
        System.out.println(tableID.getSelectionModel().getSelectedItem().getBrewery());
        System.out.println(tableID.getSelectionModel().getSelectedItem().getCountry());
    }
}

To implement this you will need to store all the table data. From your controller class initially create an object for each cell's data. First write this line top of your controller class:

要实现这一点,您需要存储所有表数据。从您的控制器类最初为每个单元格的数据创建一个对象。首先在你的控制器类的顶部写下这一行:

ObservableList<TableData> data = FXCollections.observableArrayList();

Then add all your table data using a loop. Here is an example to store one data:

然后使用循环添加所有表数据。这是存储一个数据的示例:

data.add(new TableData("Beer","Brewery","Country"));

Here is the TableDataclass:

这是TableData课程:

public class TableData
{
    String beer;
    String brewery;
    String country;

    public TableData(String beer, String brewery, String country)
    {
        super();
        this.beer = beer;
        this.brewery = brewery;
        this.country = country;
    }

    public String getBeer()
    {
        return beer;
    }
    public String getBrewery()
    {
        return brewery;
    }
    public String getCountry()
    {
        return country;
    }

}