Files
HW-07-JavaFX/src/main/java/sbu/javafx/MusicController.java
T
2026-06-21 16:40:54 +03:30

69 lines
2.0 KiB
Java

package sbu.javafx;
import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import java.util.ArrayList;
public class MusicController {
@FXML
private Label nowPlayingLabel;
@FXML
private VBox rootContainer;
private static ArrayList<String> playlist = new ArrayList<>();
@FXML
public void handlePlayAction(ActionEvent event) {
Button clickedButton = (Button) event.getSource();
String songName = (String) clickedButton.getUserData();
nowPlayingLabel.setText("Now Playing: " + songName);
}
@FXML
public void handleAddAction(ActionEvent event) {
Button clickedButton = (Button) event.getSource();
String songName = (String) clickedButton.getUserData();
addToPlaylist(songName);
}
public void addToPlaylist(String songName) {
if (!playlist.contains(songName)) {
playlist.add(songName);
}
}
@FXML
public void openPlaylistWindow() {
Stage stage = (Stage) rootContainer.getScene().getWindow();
ListView<String> listView = new ListView<>();
listView.getItems().addAll(playlist);
Button backButton = new Button("Back");
backButton.setOnAction(e -> {
try {
javafx.fxml.FXMLLoader fxmlLoader = new javafx.fxml.FXMLLoader(MusicApp.class.getResource("App-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 800, 600);
stage.setScene(scene);
} catch (Exception ex) {
ex.printStackTrace();
}
});
VBox vbox = new VBox(15, new Label("Your Playlist"), listView, backButton);
vbox.setAlignment(Pos.CENTER);
vbox.setStyle("-fx-padding: 30px;");
Scene playlistScene = new Scene(vbox, 800, 600);
stage.setScene(playlistScene);
}
}