Я дал GridPane значение для fx:id
fied в файле fxml, а затем получил доступ к этому идентификатору в коде Java с помощью функции getId
, и это отлично работает. Однако, когда я попробовал то же самое с кнопкой, getId
возвращает ноль. Возможно ли то, что я пытаюсь сделать, и если да, то как я могу сделать что-то подобное?
Это fxml-файл:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<GridPane alignment="center" hgap="40" vgap="40" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="Tills.Controllers.MoviePageController">
<GridPane fx:id="movie1Grid" alignment="center" hgap="10" vgap="10" GridPane.columnIndex="0" GridPane.rowIndex="0">
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="0">
<Label fx:id="movie1" alignment="center" text="Movie 1"/>
</HBox>
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="1" spacing="10">
<VBox alignment="center">
<Button fx:id="button0" text="17:00" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button1" text="13:15" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button2" text="13:30" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
</HBox>
</GridPane>
<GridPane fx:id="movie2Grid" alignment="center" hgap="10" vgap="10" GridPane.columnIndex="0" GridPane.rowIndex="1">
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="0">
<Label fx:id="movie2" alignment="center" text="Movie 1"/>
</HBox>
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="1" spacing="10">
<VBox alignment="center">
<Button fx:id="button3" text="13:00" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button4" text="13:15" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button5" text="13:30" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
</HBox>
</GridPane>
<GridPane fx:id="movie3Grid" alignment="center" hgap="10" vgap="10" GridPane.columnIndex="0" GridPane.rowIndex="2">
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="0">
<Label fx:id="movie3" alignment="center" text="Movie 1"/>
</HBox>
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="1" spacing="10">
<VBox alignment="center">
<Button fx:id="button6" text="13:00" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button7" text="13:15" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button8" text="13:30" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
</HBox>
</GridPane>
<GridPane fx:id="movie4Grid" alignment="center" hgap="10" vgap="10" GridPane.columnIndex="1" GridPane.rowIndex="0">
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="0">
<Label fx:id="movie4" alignment="center" text="Movie 1"/>
</HBox>
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="1" spacing="10">
<VBox alignment="center">
<Button fx:id="button9" text="13:00" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button10" text="13:15" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button11" text="13:30" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
</HBox>
</GridPane>
<GridPane fx:id="movie5Grid" alignment="center" hgap="10" vgap="10" GridPane.columnIndex="1" GridPane.rowIndex="1">
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="0">
<Label fx:id="movie5" alignment="center" text="Movie 1"/>
</HBox>
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="1" spacing="10">
<VBox alignment="center">
<Button fx:id="button12" text="13:00" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button13" text="13:15" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button14" text="13:30" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
</HBox>
</GridPane>
<GridPane fx:id="movie6Grid" alignment="center" hgap="10" vgap="10" GridPane.columnIndex="1" GridPane.rowIndex="2">
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="0">
<Label fx:id="movie6" alignment="center" text="Movie 1"/>
</HBox>
<HBox alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="1" spacing="10">
<VBox alignment="center">
<Button fx:id="button15" text="13:00" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button16" text="13:15" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
<VBox alignment="center">
<Button fx:id="button17" text="13:30" onAction="#submitMovieChoice"/>
<Label text="test screen"/>
</VBox>
</HBox>
</GridPane>
</GridPane>
Это метод контроллера, который пытается получить идентификатор:
public void submitMovieChoice(ActionEvent event) {
Button button = (Button) event.getSource();
GridPane movie = (GridPane) button.getParent().getParent().getParent();
String name = null;
// retrieve the name of the selected movie
HBox hbox = (HBox) movie.getChildren().get(0);
Label label = (Label) hbox.getChildren().get(0);
name = label.getText();
//CALLUM THIS IS WHERE THE SCREENING IS NEEDED FOR PASSING THROUGH
int screeningID = 0;
int age = 0;
if(button.getId().length() == 7) {
screeningID = screeningIDs[Integer.parseInt(button.getId().substring(button.getId().length() - 1))];
}
else
screeningID = screeningIDs[Integer.parseInt(button.getId().substring(button.getId().length() - 2, button.getId().length() - 1 ))];
try {
//Load the ticket page with the selected name and time
FXMLLoader loader = new FXMLLoader(getClass().getResource("../ticketType.fxml"));
TicketPageController controller = new TicketPageController(button.getText(), name, screeningID, age);
loader.setController(controller);
Parent parent = loader.load();
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(new Scene(parent));
window.show();
} catch (IOException e) {
System.err.println("Could not load page");
}
}