From 7c5e16de611a559ae5515f9026f41e40609943b8 Mon Sep 17 00:00:00 2001 From: HadiSharifi Date: Thu, 28 May 2026 12:10:04 +0330 Subject: [PATCH] add methods to controller --- src/main/java/sbu/javafx/Launcher.java | 2 +- src/main/java/sbu/javafx/MusicApp.java | 1 - src/main/java/sbu/javafx/MusicController.java | 68 ++++++++++++++++--- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/main/java/sbu/javafx/Launcher.java b/src/main/java/sbu/javafx/Launcher.java index 00956a4..e3e4a1d 100644 --- a/src/main/java/sbu/javafx/Launcher.java +++ b/src/main/java/sbu/javafx/Launcher.java @@ -4,6 +4,6 @@ import javafx.application.Application; public class Launcher { public static void main(String[] args) { - // TODO: Launch the JavaFX application by calling Application.launch(Main.class) } + Application.launch(MusicApp.class, args); } } \ No newline at end of file diff --git a/src/main/java/sbu/javafx/MusicApp.java b/src/main/java/sbu/javafx/MusicApp.java index a50badc..0ac846c 100644 --- a/src/main/java/sbu/javafx/MusicApp.java +++ b/src/main/java/sbu/javafx/MusicApp.java @@ -16,7 +16,6 @@ public class MusicApp extends Application { Scene scene = new Scene(fxmlLoader.load()); stage.setTitle("Music Player"); stage.setScene(scene); - //TODO: add icon to the stage stage.show(); } diff --git a/src/main/java/sbu/javafx/MusicController.java b/src/main/java/sbu/javafx/MusicController.java index 48e1774..867165d 100644 --- a/src/main/java/sbu/javafx/MusicController.java +++ b/src/main/java/sbu/javafx/MusicController.java @@ -1,37 +1,85 @@ package sbu.javafx; +import javafx.beans.Observable; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.event.ActionEvent; +import javafx.scene.control.Button; import javafx.scene.control.Label; +import javafx.scene.control.ListView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.VBox; +import java.io.File; + public class MusicController { - // TODO: Define your UI components using the @FXML annotation. + @FXML Button playButton; + private boolean isPlaying = false; + @FXML private ListView musicListView; + private ObservableList musicList = FXCollections.observableArrayList(); + private ObservableList playList = FXCollections.observableArrayList(); - //TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing. + @FXML + public void initialize() { + File folder = new File("musics"); + for (File file : folder.listFiles()) { + if (file.getName().endsWith(".mp3")) { + String[] str = file.getName().split("-"); + String songName = str[0]; + String artist = str[1]; + musicList.add(new Song(songName,artist,file.getAbsolutePath())); + } + } + + musicListView.setItems(musicList); + } @FXML public void handlePlayAction() { - //Update labels, change button icons, etc. + if (isPlaying) { + isPlaying = false; + playButton.setText("play"); + } + else { + isPlaying = true; + playButton.setText("pause"); + } } @FXML - public void handlePrevious() {} + public void handlePrevious() { + int currentIndex = musicListView.getSelectionModel().getSelectedIndex(); + if (currentIndex > 0) { + musicListView.getSelectionModel().select(currentIndex - 1); + handlePlayAction(); + } + } @FXML - public void handlenext() {} + public void handlenext() { + int currentIndex = musicListView.getSelectionModel().getSelectedIndex(); + if (currentIndex < musicListView.getItems().size() - 1) { + musicListView.getSelectionModel().select(currentIndex + 1); + handlePlayAction(); + } + } - //TODO: Logic to add a specific song to the user's playlist. @FXML public void addToPlaylist() { - //Add the song to a list or update a ListView. + Song song = musicListView.getSelectionModel().getSelectedItem(); + if (song != null && !playList.contains(song)) { + playList.add(song); + } } - // TODO:This method should open a new window or change the current scene to display the "Playlist" page. - + @FXML public void openPlaylistWindow() { - //Create a new stage and show the playlist UI. + musicListView.setItems(playList); + } + @FXML + public void openAllSongs() { + musicListView.setItems(musicList); } }