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.
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());
}
});
}
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With