add methods to controller

This commit is contained in:
2026-05-28 12:10:04 +03:30
parent 21a2e95cd7
commit 7c5e16de61
3 changed files with 59 additions and 12 deletions
+1 -1
View File
@@ -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);
}
}
-1
View File
@@ -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();
}
+58 -10
View File
@@ -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<Song> musicListView;
private ObservableList<Song> musicList = FXCollections.observableArrayList();
private ObservableList<Song> 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);
}
}