我正在用JavaFX制作歌曲库,并且需要有关我的编辑功能的帮助。从列表视图中选择一首歌曲后,我想切换到新场景,并将旧歌曲的数据加载到文本字段(歌曲名称,艺术家等)中,但是我总是以空值结尾。我尝试通过解决方案here,但仍然无法使其正常工作。
这是我的代码(由于无效,我在editPromptController中清空了初始化):
package jFiles.Controllers;
import jFiles.Main;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.actionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import resources.data.songData;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
public class libraryController {
public Button add_button;
public Button edit_button;
public Button delete_button;
public SplitPane wholeWindow;
private ObservableList<String> observable_track_list = FXCollections.observableArrayList();
public AnchorPane songSideBar;
@FXML
public ListView<String> song_list = new ListView<>();
private ArrayList<String> listed_tracks = new ArrayList<>();
public Label song_label;
public Label artist_label;
public Label album_label;
public Label year_label;
private File file = new File("src/resources/data/songData.txt");
private File detailsFile = new File("src/resources/data/songDetails.txt");
// Takes user to the "add a song" prompt
public void add_song(MouseEvent mouseEvent) throws IOException {
Parent addRoot = FXMLLoader.load(Objects.requireNonNull(getclass().getclassLoader().getResource("resources/views/add_prompt.fxml")));
Scene add_song = new Scene(addRoot);
Stage add_song_prompt = (Stage) ((Node) mouseEvent.getsource()).getScene().getWindow();
add_song_prompt.setScene(add_song);
add_song_prompt.show();
}
// Takes user to the "edit song" prompt
public void edit_song(MouseEvent mouseEvent) throws IOException {
Parent editRoot = FXMLLoader.load(Objects.requireNonNull(getclass().getclassLoader().getResource("resources/views/edit_prompt.fxml")));
Scene edit_song = new Scene(editRoot);
Stage edit_song_prompt = (Stage) ((Node) mouseEvent.getsource()).getScene().getWindow();
edit_song_prompt.setScene(edit_song);
edit_song_prompt.show();
}
public void delete_song(MouseEvent mouseEvent) throws IOException {
}
// Scans through the text file (mock database) containing the listed tracks
private void scanFile() throws FileNotFoundException {
Scanner scan = new Scanner(file);
song_list.setItems(observable_track_list);
while (scan.hasnextLine()) {
String song = scan.nextLine();
if (!listed_tracks.contains(song)) {
observable_track_list.add(song);
listed_tracks.add(song);
}
}
Collections.sort(observable_track_list);
}
// Scans through,parses,and returns the full details of the selected track
private String[] getDetails(String selected) throws FileNotFoundException {
Scanner scan = new Scanner(detailsFile);
String[] details = new String[4];
while (scan.hasnextLine()) {
String song_line = scan.nextLine();
if (song_line.contains(selected)) {
details = song_line.split(" - ");
}
}
return details;
}
// Returns the existing tracks
public ArrayList<String> getList() throws FileNotFoundException {
scanFile();
return listed_tracks;
}
// Loads the existing tracks
@FXML
public void initialize() throws FileNotFoundException {
scanFile();
song_list.getSelectionmodel().select(0);
fill_in_details();
}
// Loads the details of the selected song in the main display
private void fill_in_details() throws FileNotFoundException {
String[] details = getDetails(song_list.getSelectionmodel().getSelectedItem());
song_label.setText(details[0]);
artist_label.setText(details[1]);
album_label.setText(details[2]);
year_label.setText(details[3]);
}
public void item_selection(MouseEvent mouseEvent) throws FileNotFoundException {
fill_in_details();
System.out.println(song_list.getSelectionmodel().getSelectedItem());
}
}
package jFiles.Controllers;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;
public class editPromptController {
public TextField song_name;
public TextField artist_name;
public TextField album_name;
public TextField song_year;
public void edit(MouseEvent mouseEvent) {
//libraryController test = new libraryController();
//System.out.println(test.song_list.getSelectionmodel().getSelectedItem());
}
public void cancel(MouseEvent mouseEvent) throws IOException {
Parent libraryRoot = FXMLLoader.load(Objects.requireNonNull(getclass().getclassLoader().getResource("resources/views/libraryDisplay.fxml")));
Scene library = new Scene(libraryRoot);
Stage libraryWindow = (Stage) ((Node) mouseEvent.getsource()).getScene().getWindow();
libraryWindow.setScene(library);
libraryWindow.show();
}
@FXML
public void initialize() throws FileNotFoundException {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane minHeight="400.0" minWidth="600.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="jFiles.Controllers.libraryController">
<children>
<SplitPane fx:id="wholeWindow" dividerPositions="0.29797979797979796" prefHeight="400.0" prefWidth="600.0">
<items>
<AnchorPane fx:id="songSideBar" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label prefHeight="50.0" prefWidth="175.0" text=" Song List" />
<ListView fx:id="song_list" layoutY="50.0" onmouseclicked="#item_selection" prefHeight="300.0" prefWidth="175.0" />
<Button fx:id="add_button" layoutX="14.0" layoutY="359.0" mnemonicParsing="false" onmouseclicked="#add_song" text="Add" />
<Button fx:id="edit_button" layoutX="69.0" layoutY="359.0" mnemonicParsing="false" onmouseclicked="#edit_song" text="Edit" />
<Button fx:id="delete_button" layoutX="117.0" layoutY="359.0" mnemonicParsing="false" onmouseclicked="#delete_song" text="Delete" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutX="30.0" layoutY="30.0" text="NOW PLAYING:" />
<Pane layoutX="160.0" layoutY="120.0" prefHeight="190.0" prefWidth="157.0">
<children>
<Label fx:id="song_label" layoutX="65.0" layoutY="6.0" text="Label" />
<Label fx:id="artist_label" layoutX="65.0" layoutY="62.0" text="Label" />
<Label fx:id="album_label" layoutX="65.0" layoutY="115.0" text="Label" />
<Label fx:id="year_label" layoutX="65.0" layoutY="168.0" text="Label" />
</children></Pane>
<Label layoutX="112.0" layoutY="126.0" text="Song:" />
<Label layoutX="113.0" layoutY="182.0" text="Artist:" />
<Label layoutX="114.0" layoutY="235.0" text="Album:" />
<Label layoutX="119.0" layoutY="289.0" text="Year:" />
</children></AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="jFiles.Controllers.editPromptController">
<children>
<TextField fx:id="song_name" layoutX="211.0" layoutY="96.0" />
<TextField fx:id="artist_name" layoutX="211.0" layoutY="152.0" />
<TextField fx:id="album_name" layoutX="211.0" layoutY="203.0" promptText="Optional" />
<TextField fx:id="song_year" layoutX="211.0" layoutY="261.0" promptText="Optional" />
<Label layoutX="126.0" layoutY="100.0" text="Song Name:" />
<Label layoutX="159.0" layoutY="156.0" text="Artist:" />
<Label layoutX="152.0" layoutY="207.0" text="Album:" />
<Label layoutX="161.0" layoutY="265.0" text="Year:" />
<Button fx:id="edit_button" layoutX="192.0" layoutY="320.0" mnemonicParsing="false" onmouseclicked="#edit" text="Submit Edit" />
<Button fx:id="cancel_button" layoutX="334.0" layoutY="320.0" mnemonicParsing="false" onmouseclicked="#cancel" text="Cancel" />
</children>
</AnchorPane>