I am writing a music player that can be played in background while users do their thing. The problem is when I exit the music player panel into other panel, the music can't be stopped anymore.
Here is my code
JButton btnAddMusic = new JButton("add music");
btnAddMusic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
returnValue=browser.showOpenDialog(main);
if(returnValue==browser.APPROVE_OPTION){
selectedFile=browser.getSelectedFile();
String filepath=selectedFile.toString();
String filename=selectedFile.getName();
main.getController().storeMusic(filename, filepath);
populateJComboBox();
}
}
});
btnAddMusic.setBounds(15, 187, 115, 29);
add(btnAddMusic);
JButton btnlogout = new JButton("");
btnlogout.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 19));
btnlogout.setBackground(UIManager.getColor("Button.background"));
Image aimg= new ImageIcon(this.getClass().getResource("/logot.png")).getImage();
Image aImage = aimg.getScaledInstance(55, 55, Image.SCALE_DEFAULT);
btnlogout.setIcon(new ImageIcon(aImage));
btnlogout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
main.showRegisterPanel();
}
});
btnlogout.setForeground(UIManager.getColor("Button.foreground"));
btnlogout.setBackground(Color.WHITE);
btnlogout.setOpaque(true);
btnlogout.setBorderPainted(false);
btnlogout.setBounds(411, 0, 64, 53);
add(btnlogout);
JLabel icon = new JLabel("");
icon.setBounds(15, 16, 93, 81);
Image img= new ImageIcon(this.getClass().getResource("/tplogo.png")).getImage();
Image newImage = img.getScaledInstance(70, 70, Image.SCALE_DEFAULT);
icon.setIcon(new ImageIcon(newImage));
add(icon);
JLabel lblNewLabel = new JLabel("Music Player");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblNewLabel.setBounds(155, 29, 276, 53);
add(lblNewLabel);
this.btnhome = new JButton("");
btnhome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
check();
}
});
btnhome.setBackground(Color.WHITE);
btnhome.setBounds(180, 232, 75, 63);
Image home= new ImageIcon(this.getClass().getResource("/home.png")).getImage();
Image ahome = home.getScaledInstance(55, 55, Image.SCALE_DEFAULT);
btnhome.setIcon(new ImageIcon(ahome));
btnhome.setForeground(UIManager.getColor("Button.foreground"));
btnhome.setBackground(Color.WHITE);
btnhome.setOpaque(true);
btnhome.setBorderPainted(false);
add(btnhome);
JButton btnPlay = new JButton("play");
btnPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
for(int i=0;i<1024;i++){
try{
if(comboBox.getSelectedIndex()==i){
sound=new File(music[comboBox.getSelectedIndex()]);
ais=AudioSystem.getAudioInputStream(sound);
clip=AudioSystem.getClip();
clip.open(ais);
clip.start();
}
}catch(Exception e){
}
}
}});
btnPlay.setBounds(170, 187, 115, 29);
add(btnPlay);
JButton btnStop = new JButton("stop");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clip.stop();
}
});
btnStop.setBounds(320, 187, 115, 29);
add(btnStop);
browser.setFileFilter(filter);
this.comboBox = new JComboBox();
comboBox.setBounds(77, 111, 262, 26);
add(comboBox);
populateJComboBox();
}
private void populateJComboBox() {
this.mu = this.main.getController().getAllMusic(); //get hardcoded cluster details
DefaultComboBoxModel model = new DefaultComboBoxModel();
for(int i = 0; i<mu.length; i++) // for loop to add the cluster details into the comboBox model
{
Music clu = mu[i];
model.addElement(clu.getMname());
music[i]=clu.getMc();
}
this.comboBox.setModel(model); //setting the comboBox model with the model with cluster details added
}
private void check(){
this.user = this.main.getController().getAllUsers();
for(int i = 0; i<user.length;i++)
{
User use = user[i];
String aa = use.getType();
if(aa=="Student"){
main.showSthomepanel();
}
else if(aa=="Staff"){
main.showshomePanel();
}
else{
btnhome.setEnabled(false);
}
}
The initialise is done, but I can't post because of this weird code system.
This problem is caused by the focus being taken away from the panel. The easiest way to fix this is to use a JWindow instead of a JPanel.