2

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(){

}
}
newbie
  • 101
  • 1
  • 8

3 Answers3

1

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(){
    
    }
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • That's exactly what I want. I really appreciate that, but why we have to initialize another variable to save it? – newbie Jan 09 '19 at 04:27
  • @TriDo Because the variable `col` and `countRow`'s value is changing in the loop whereas value of `finalCol` or `finalCountRow` is assigned only once, that makes them effectively finel. –  Jan 09 '19 at 04:28
0

Why would you get text from toggle? Anyways, It has only two states true or false. We can use the states to infer automatically. Toggle buttons only give you the state, but cannot give a text.

If you are specific to hold a text, you should probably go with dropdown.

Mohamed Anees A
  • 4,119
  • 1
  • 22
  • 35
  • Hi, I want to get the text from toggle button so that I can add it into the List in order to check if the seats are already booked or not. if the seats are being booked, the button will be set disable. – newbie Jan 09 '19 at 04:21
0

If you want to have a value associated with a ToggleButton you should use the setUserData method for the buttons. This can then be fetched back by using the getUserData method.

Calvin Iyer
  • 161
  • 4