Files
HW-07-JavaFX/src/main/java/sbu/javafx/MusicController.java
T
2026-06-09 13:58:56 +03:30

128 lines
3.6 KiB
Java

package sbu.javafx;
//main
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
//event
import javafx.event.ActionEvent;
//UI controller
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
//layout
import javafx.scene.layout.VBox;
//stage and scene
import javafx.stage.Stage;
import javafx.scene.Scene;
import java.util.ArrayList;
import java.io.IOException;
public class MusicController {
@FXML private Label nowPlayingLabel;
@FXML private VBox playlistContainer;
@FXML private ListView<String> playlistView;
private ArrayList<String> playlist = new ArrayList<>();
private ArrayList<Song> songList = new ArrayList<>();
private Stage primaryStage;
public void setPrimaryStage(Stage stage) {
this.primaryStage = stage;
}
//TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.
@FXML
public void handlePlayAction(ActionEvent event) {
//Update labels, change button icons, etc.
String songName;
Button clickedButton = (Button) event.getSource();
String buttonText = clickedButton.getText();
if(clickedButton.getId() != null){
switch (clickedButton.getId()){
case "playM1" :
songName = "Song of Dawn";
break;
case "playM2" :
songName = "Midnight Drive";
break;
case "playM3" :
songName = "Echoes of You";
break;
default:
songName = "Unknown Song";
}
}
else
songName = "Selected Song";
nowPlayingLabel.setText("Now playing: " + songName);
}
//TODO: Logic to add a specific song to the user's playlist.
@FXML
public void addToPlaylist(String songName) {
//Add the song to a list or update a ListView.
playlist.add(songName);
nowPlayingLabel.setText(songName + " added to playlist :)");
if (playlistView != null) {
playlistView.getItems().add(songName);
}
}
@FXML
public void handleAddToPlaylist (ActionEvent event){
Button clickButton = (Button) event.getSource();
String songName;
if(clickButton.getId() != null){
switch (clickButton.getId()){
case "addM1" :
songName = "Song of Dawn | Alice Johnson | 03:45";
break;
case "addM2" :
songName = "Midnight Drive | The Night Owls | 04:12";
break;
case "addM3" :
songName = "Echoes of You | Luna Sky | 05:08";
break;
default:
songName = "Unknown Song";
}
}
else
songName = "Selected Song";
addToPlaylist(songName);
}
// TODO:This method should open a new window or change the current scene to display the "Playlist" page.
@FXML
public void openPlaylistWindow() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("playlist-view.fxml"));
Scene scene = new Scene(loader.load());
PlaylistController pc = loader.getController();
pc.setPlaylist(playlist);
Stage stage = new Stage();
stage.setScene(scene);
stage.getIcons().add(new Image(getClass().getResourceAsStream("icon.png")));
stage.show();
}
}