2

I'd like to mute my music player player using player.mute() when the JCheckBox mutemusic_box is checked. If its unchecked and the player is muted, the command player.mute() should execute. How to get this working? My code currently does not work (may because of the ActionEvent).

The check if it's checked or not is in actionPerformed.

private JCheckBox pwremember_box, mutemusic_checkbox;

//GUI setup

public void actionPerformed(ActionEvent ae) {

    if (mutemusic_checkbox.isSelected()) {
        System.out.println("Muted!");
        player.mute();
    }
    else {
      System.out.println("Unmuted!");
      player.mute();
    } 
}
xenteros
  • 15,586
  • 12
  • 56
  • 91
realKSMC
  • 115
  • 11
  • I am new to java. Feel free to comment so I can improve my asking skills. May the issue is in the ActionEvent. – realKSMC Jul 30 '17 at 17:05
  • Possible duplicate of [How to check that a JCheckBox is checked?](https://stackoverflow.com/questions/3599908/how-to-check-that-a-jcheckbox-is-checked) – Jonny Henly Jul 30 '17 at 17:12
  • you must `addActionListener` (conatining the `actionPerformed`) to your checkbox, else the GUI would not *know* that the `actionPerformed` should be called. – user85421 Jul 30 '17 at 17:29
  • Could you post this as an answer? I do not know how to get this done. – realKSMC Jul 30 '17 at 17:31
  • see Oracle's tutorial [How to Use Buttons, Check Boxes, and Radio Buttons](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html), these are pretty good to learn the basics and how classes are intended to be used. And here at the right there are at least to links that could help (under **Linked**) ==> – user85421 Jul 30 '17 at 17:40
  • or just look at [davidxxx' answer](https://stackoverflow.com/a/45402509/85421) below, he just added it - - - but I still recommend the tutorials! – user85421 Jul 30 '17 at 17:45

3 Answers3

4

First this check is not required :

if (ae.getSource().equals(mutemusic_box)) {

The ActionListener should be associated to the JCheckBox button.
So, there is not any ambiguity.

Then in order to know if the checkbox is checked, use simply the isSelected() method that is designed for.

Besides mute() is not a good named method to toggle the sound of the Player.
toggleSound() could make more sense.

And as explained in my comment, you have to attach first the listener to the JCheckBox.

Here is a sample code :

checkBoxMute.addActionListener(
   new ActionListener(){
        public void actionPerformed(ActionEvent ae) {   
          if (checkBoxMute.isSelected()) {
              System.out.println("Muted!");
              player.toggleSound();
          }
          else {
              System.out.println("Unmuted!");
              player.toggleSound();
          } 
       }
   });

And here the same thing with a lambda instead of creating an anonymous class of ActionListener :

checkBoxMute.addActionListener(
    ae -> {
        if (checkBoxMute.isSelected()) {
          System.out.println("Muted!");
          player.toggleSound();
        } else {
          System.out.println("Unmuted!");
          player.toggleSound();
        }
    });
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

You can use isSelected method.

boolean isChecked = mutemusic_box.isSelected();

As a personal advice, rename mutemusic_box to muteMusicCheckBox or similar.

xenteros
  • 15,586
  • 12
  • 56
  • 91
-1

You can try

mutemusic_box.isSelected();
Akash
  • 587
  • 5
  • 12
  • 2
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Jul 30 '17 at 22:51