点击电影,我将显示有关电影的详细信息 列表视图。 当前,当我单击影片时什么也没有发生。 有谁知道我如何做到这一点? 我不知道该怎么做。 我的代码包含在下面。
控制器:
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import no.hiof.aleksar.oblig4.data.DataHandler;
import no.hiof.aleksar.oblig4.model.Film;
public class FilmController {
@FXML
private Label navnFilmLabel;
@FXML
private TextField beskrivelseTextField, utgivelsesdatoTextField, spilletidTextField;
@FXML
private ListView listeMedFilmer;
private int valgtFilm = 0;
private Film denValgteFilmen;
private ObservableList<Film> filmListe;
@FXML
public void initialize() {
listeMedFilmer.setItems(DataHandler.hentFilmData());
filmListe = DataHandler.hentFilmData();
if (valgtFilm < filmListe.size()) {
Film enFilm = filmListe.get(valgtFilm);
fyllUtFilm(enFilm);
}
}
private void fyllUtFilm(Film enFilm) {
if (enFilm != null) {
navnFilmLabel.setText(enFilm.getTittel());
beskrivelseTextField.setText(enFilm.getBeskrivelse());
utgivelsesdatoTextField.setText(enFilm.getUtgivelsesdato().toString());
spilletidTextField.setText(String.valueOf(enFilm.getSpilletid()));
}
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.hiof.aleksar.oblig4.controller.FilmController">
<children>
<ListView fx:id="listeMedFilmer" prefHeight="400.0" prefWidth="291.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="300.0" AnchorPane.topAnchor="0.0" />
<Button layoutX="310.0" layoutY="373.0" mnemonicParsing="false" text="Ny" />
<Button layoutX="348.0" layoutY="373.0" mnemonicParsing="false" text="Rediger" />
<Button layoutX="412.0" layoutY="373.0" mnemonicParsing="false" text="Slett" />
<Label fx:id="navnFilmLabel" layoutX="312.0" layoutY="14.0" text="Film/Serie/Episode">
<font>
<Font name="System Bold" size="27.0" />
</font>
</Label>
<GridPane layoutX="312.0" layoutY="68.0" prefHeight="208.0" prefWidth="276.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Beskrivelse:" />
<Label text="Utgivelsesdato:" GridPane.rowIndex="1" />
<Label text="Spilletid:" GridPane.rowIndex="2" />
<TextField fx:id="beskrivelseTextField" prefHeight="50.0" prefWidth="138.0" GridPane.columnIndex="1">
<font>
<Font size="10.0" />
</font></TextField>
<TextField fx:id="utgivelsesdatoTextField" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="10.0" />
</font></TextField>
<TextField fx:id="spilletidTextField" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font size="10.0" />
</font></TextField>
</children>
</GridPane>
</children>
</AnchorPane>
数据处理程序:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import no.hiof.aleksar.oblig4.model.Film;
import no.hiof.aleksar.oblig4.model.Person;
import java.time.LocalDate;
public class DataHandler {
private final static ObservableList<Film> filmListe = FXCollections.observableArrayList();
public static ObservableList<Film> hentFilmData() {
if (filmListe.isEmpty()) {
genererFilmData();
}
return filmListe;
}
private static void genererFilmData() {
filmListe.add(new Film("Inception", "A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a CEO." , 148, LocalDate.of(2010, 7, 21), new Person("Leonardo", "DiCaprio", LocalDate.of(1974, 11, 11))));
filmListe.add(new Film("The Imitation Game", "During World War II, the English mathematical genius Alan Turing tries to crack the German Enigma code with help from fellow mathematicians." , 114, LocalDate.of(2014, 12, 25), new Person("Benedict", "Cumberbatch", LocalDate.of(1976, 7, 19))));
filmListe.add(new Film("Jobs", "The story of Steve Jobs' ascension from college dropout into one of the most revered creative entrepreneurs of the 20th century." , 128, LocalDate.of(2013, 11, 1), new Person("Ashton", "Kutcher", LocalDate.of(1978, 2, 7))));
}
}