0

I want to have the client load the player's favorite songs from the text file and put each line

The entire class is here:

http://pastebin.com/TeMk3Nft (Because if I posted it here it would be too much code vs text)

But I'm not really sure how to do it

p.s I'm not really sure what the loop is supposed to iterate over(Line 104)

StanislavL
  • 56,971
  • 9
  • 68
  • 98
Ravekitty
  • 77
  • 1
  • 14

1 Answers1

1

Your class is massive and it is missing other classes references.Though,I have put together an example for you. I am sure this will help you.

import java.awt.EventQueue;

public class SongPlayer {

    private JFrame frmSongPlayer;
    private List<String> songs;
    private ActionListener listener = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JMenuItem) {
                JMenuItem item = (JMenuItem) e.getSource();
                // now
                String url = "http://songs/" + item.getName();
                System.out.println(url);
            }

        }
    };

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SongPlayer window = new SongPlayer();
                    window.frmSongPlayer.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public SongPlayer() {
        try {
            songs = FileUtils.readLines(new File(SongPlayer.class.getResource("/PlayList.txt").getPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmSongPlayer = new JFrame();
        frmSongPlayer.setTitle("Song player");
        frmSongPlayer.setBounds(100, 100, 450, 300);
        frmSongPlayer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmSongPlayer.getContentPane().setLayout(null);

        JMenuBar songBar = new JMenuBar();
        songBar.setBounds(10, 11, 101, 23);
        frmSongPlayer.getContentPane().add(songBar);

        JMenu song = new JMenu("Songs");

        songBar.add(song);
        for (String mp3song : songs) {
            JMenuItem mntmNewMenuItem = new JMenuItem(mp3song);
            mntmNewMenuItem.setName(mp3song);
            mntmNewMenuItem.addActionListener(listener);

            song.add(mntmNewMenuItem);
        }

    }
}

Above class will open Swing UI with songs menu and the items are selected from Playlist.txt file.When you click on the song,it generates appropriate url.

Makky
  • 17,117
  • 17
  • 63
  • 86