Issue
I am new to JavaFX. I'm working on the movie booking project. I have tried to retrieve the text from toggle button after looping it, but I don't see the getText() method. Is there any way to get the text from toggle button.
Here is my code:
package theatre;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.GridPane;
import java.net.URL;
import java.util.ResourceBundle;
public class TheatreController implements Initializable {
@FXML
private GridPane middleGridPane;
private final int MAX_ROW = 5;
private final int MAX_COL = 5;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
int countRow = 1;
ToggleButton[][] seats = new ToggleButton[MAX_ROW][MAX_COL];
char lastRow = 'A' + (MAX_ROW - 1);
for(char row = 'A'; row < lastRow; row++){
for (int col = 1; col < MAX_COL; col++){
seats[countRow][col] = new ToggleButton(String.format(row + "%02d", col));
seats[countRow][col].setPrefWidth(50);
middleGridPane.add(seats[countRow][col], countRow, col);
seats[countRow][col].setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// Can't get text from here
event.getEventType().getName();
}
});
}
countRow++;
}
}
@FXML
public void bookSeat(){
}
}
Solution
Any variable that you use in handle()
of EventHandler
must be final
or effectively finel.
Here is solution for your problem:
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.GridPane;
import java.net.URL;
import java.util.ResourceBundle;
public class TheatreController implements Initializable {
@FXML
private GridPane middleGridPane;
private final int MAX_ROW = 5;
private final int MAX_COL = 5;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
int countRow = 1;
ToggleButton[][] seats = new ToggleButton[MAX_ROW][MAX_COL];
char lastRow = 'A' + (MAX_ROW - 1);
for(char row = 'A'; row < lastRow; row++){
for (int col = 1; col < MAX_COL; col++){
seats[countRow][col] = new ToggleButton(String.format(row + "%02d", col));
seats[countRow][col].setPrefWidth(50);
middleGridPane.add(seats[countRow][col], countRow, col);
//effectively final variable to use in handle method of EventHandler
int finalCountRow = countRow;
int finalCol = col;
seats[countRow][col].setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
//now you can get text from seats[finalCountRow][finalCol]
System.out.println(seats[finalCountRow][finalCol].getText());
event.getEventType().getName();
}
});
}
countRow++;
}
}
@FXML
public void bookSeat(){
}
}
Answered By - user10485339