From 427c648e3307e776eaa95827cb74dc97547b3754 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Wed, 24 Feb 2016 15:59:23 +0000 Subject: [PATCH] Play button now starts from selected song in playlist. --- src/main/java/musicplayer/PlayerGUI.java | 5 ++++- src/main/java/musicplayer/PlaylistTableModel.java | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index 2a627c9..31e79c6 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -66,7 +66,10 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf //Action Listeners playButton.addActionListener(e -> { if (playList.getRowCount() > 0) { - player.playSong(playlistTableModel.getFirst()); + if(playList.getSelectedRowCount() > 0) + player.playSong(playlistTableModel.getSong(playList.getSelectedRow())); + else + player.playSong(playlistTableModel.getFirst()); } }); MouseListener mouseListener = new libraryMouseAdapter(); diff --git a/src/main/java/musicplayer/PlaylistTableModel.java b/src/main/java/musicplayer/PlaylistTableModel.java index b9cfe09..bd699b9 100644 --- a/src/main/java/musicplayer/PlaylistTableModel.java +++ b/src/main/java/musicplayer/PlaylistTableModel.java @@ -99,4 +99,8 @@ public class PlaylistTableModel extends AbstractTableModel { songList.remove(song); fireTableDataChanged(); } + + public Optional getSong(int index){ + return songList.size() > 0 && index >= 0 && index < songList.size() ? Optional.of(songList.get(index)) : Optional.empty(); + } }