Search code examples
javaswingcomboboxjcomboboxjavasound

JComboBox in Java


I am trying to play music in my app. I am able to play when i click on Play button but not able to change the song when i choose the secon option in the comboBox. Please help look into this code and point out the issue.

public class Musika extends JPanel implements ActionListener,ItemListener{



JComboBox<String>comboCanciones;

String canciones[]= {"La Moda", "Perro" };

JButton bMusicaStart = new JButton();
JButton bMusicaStop = new JButton();

Clip clip;
String cancionEle ;

public Musika(Pantalla pantalla, boolean estadoCandado) {
    ...
}

private Component crearPanelPrincipal()  {
    ...
}

private Component crearPanelCentral() {
    JSplitPane panel= new JSplitPane (JSplitPane.HORIZONTAL_SPLIT,
            crearPanelLista(),
            crearPanelReproductor());

    panel.setBorder(BorderFactory.createEmptyBorder(20,10,20,10));
    return panel;
}



private Component crearPanelReproductor() {
    JPanel panel = new JPanel (new BorderLayout());
    panel.add(panelCentro(), BorderLayout.CENTER);
    return panel;
}


private Component panelCentro() {
    JPanel panel = new JPanel (new FlowLayout());
    panel.add(crearPanelMusica());
    return panel;
}


private Component crearPanelMusica() {
     JPanel panel = new JPanel (new GridLayout(1,2,10,10));
    panel.setBorder(BorderFactory.createEmptyBorder(50,20,20,20));
    bMusicaStart.setIcon(musicaStart);
    bMusicaStart.setPreferredSize(new Dimension(50, 60));
    panel.add(bMusicaStart);
    bMusicaStart.addActionListener(this);

    bMusicaStop.setIcon(musicaStop);
    panel.add(bMusicaStop);
    bMusicaStop.addActionListener(this);
    return panel;
}


//Music is working fine
public void ReproducirSonido( ){

       try {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(newFile(cancionEle).getAbsoluteFile());
         clip = AudioSystem.getClip();
        clip.open(audioInputStream);


       } catch(UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
         System.out.println("Error al reproducir el sonido.");
       }
     }

I think everything above is fine. The error is below in my opinion

private Component crearPanelLista() {
    JPanel panel = new JPanel(new BorderLayout());
    comboCanciones = new JComboBox<>(canciones);
    panel.add(comboCanciones, BorderLayout.NORTH);
    comboCanciones.setSelectedIndex(0);
    comboCanciones.addItemListener(this);
    return panel;
}

@Override
public void actionPerformed(ActionEvent e) {

    if(e.getSource() == bMusicaStart){
            clip.start();
        }

    if(e.getSource() == bMusicaStop){
        clip.stop();
        }
    }

@Override
public void itemStateChanged(ItemEvent e) {
    int indiceSeleccionado = comboCanciones.getSelectedIndex();
    switch (indiceSeleccionado) {
    case 0: cancionEle =  "musica/laModa.wav" ;break;
    case 1: cancionEle = "musica/perro.wav";
    }
}
}

Solution

    1. method names should NOT start with an upper case character. Learn and follow Java naming conventions. Most your your method are correct but not all. Be consistent.

    2. I would guess you need to invoke the reproducirSonido( ) method in the ItemListener of the combo box to reset the audio file you want to play.