How to add a click event to a tableview cell in javafx [duplicate]
Asked Answered
K

1

5

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

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());        
        }
    });
}
Kunin answered 30/4, 2016 at 2:47 Comment(2)
Do you mean that you do not see the println when clicking? Your method looks a bit peculiar. Could you also post the code fragment where it is called? And what is tableID? It looks a bit like when you click on an item you register the event handler.Dionnadionne
Yes once clicked, I do not see the println. I adjusted the code but still no luck. This is simply just to test the click function as I am using a database, once It detects two clicks I will pull info from the database into the java app and display it in a text area.Kunin
E
13

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

@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 TableData class:

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

}
Eanore answered 30/4, 2016 at 13:7 Comment(5)
Im going to try it when I get to my pc and let you know. And for the table I already have one constructed thank you though for the class!Kunin
No luck with this, still dont see the println. Im not too sure whats wrong with this that its not being printed or its not getting the selected cell.Kunin
EDIT*** I apologzie, this works perfectly. I was saving my scenebuilder fxml file to a different location and didnt realize until now. Thank you very much! But out of curiosity, what would I change in order for it to return only beer name value when its double clicked. For example If i click any other column itll return beer name even if i clicked under brewery or country. Thank youKunin
tableID.getSelectionModel().getSelectedItem() return all the data available on that row/cell (in your case Beer, Brewery, Country, ABV & Style). When we are adding .getBeer(), it is returning the string value of Beer, which we stored by creating object of TableData class. So, when you will click on any column of that row, you will get only the string value of Beer column by this line: tableID.getSelectionModel().getSelectedItem().getBeer()Eanore
Yeah but once clicked on another column, for example under abv, it would still return the beer name which I only wanted returning when its clicked under the beer name column. But I figured it out through some attempts. Thank you for everything though you were very helpful!Kunin

© 2022 - 2024 — McMap. All rights reserved.