Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: JavaFx application not working as expected!

  1. #1
    Junior Member
    Join Date
    Nov 2024
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation JavaFx application not working as expected!

    Hi guys

    I'm currently doing chapter on Javafx for my course, and the assignment is asking us to make a javafx program that has radio buttons which change the colour of a square, a slider, and if the user clicks anywhere other than the radio buttons and slider, it should play a warning sound.

    I did it all and its working except for the part that says the warning audio should not sound if the user clicks on the radio buttons or the slider. Its not working no matter what I do. It plays the audio wherever I click on the window, even when clicking on the radio buttons and slider. I don't know what I'm doing wrong and would appreciate the help! Here is my code:

     package application;
     
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.RadioButton;
    import javafx.scene.control.Slider;
    import javafx.scene.control.ToggleGroup;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.scene.media.*;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.text.Font;
    import javafx.scene.text.FontWeight;
    import javafx.scene.text.Text;
    import javafx.scene.text.TextAlignment;
    import javafx.stage.Stage;
     
    public class ChangingShape extends Application {
     
    	private RadioButton redButton, greenButton, orangeButton;
    	private Rectangle square = new Rectangle(100,100,Color.RED);
    	private AudioClip audio = new AudioClip ("file:audio.mp3");
     
    	public static void main (String[] args) {
    		launch(args);
    	}
     
    	public void start(Stage primaryStage) {
     
    		// Create the root BorderPane
    		BorderPane root = new BorderPane();
    		root.setPadding(new Insets(10));
    		root.setStyle("-fx-background-color: lightyellow;");
     
    		// Add instructions to the top
    		root.setTop(instructions());
     
    		// Add radio buttons to the left
    		root.setLeft(radiobuttons());
     
    		// Add the square to the center
    		root.setCenter(setShape());
     
    		// Combine the slider and warning into a VBox
    		VBox sliderAndWarning = new VBox(10);  // Set some space between the slider and the warning
    		sliderAndWarning.setAlignment(Pos.CENTER);
    		sliderAndWarning.getChildren().addAll(setSlider(), warning());
     
    		// Add the combined VBox to the bottom
    		root.setBottom(sliderAndWarning);
     
    		// Add a mouse click event filter to play a warning sound when the user clicks 
    		// anywhere outside the controls (radio buttons and slider).
    		root.addEventFilter(MouseEvent.MOUSE_CLICKED, this::processWarningAudio);
     
    		Scene scene = new Scene (root, 400, 400);
    		primaryStage.setTitle("Changing Square");
    		primaryStage.setScene(scene);
    		primaryStage.show();
    	}
     
    	// Setting up the instructions at the top of the window
    	private VBox instructions() {
     
    		Text instruction = new Text ("Change the square color using the radio buttons.");
    		Text instruction_two = new Text ("Change the scale of the square between 0-100% using the slider.");
    		instruction.setFont(Font.font("Calibri", FontWeight.BOLD, 14));
    		instruction_two.setFont(Font.font("Calibri", FontWeight.BOLD, 14));
     
    		//setting up the text into a text box and aligning it to the top of the window
    		VBox textBox = new VBox();
    		textBox.setAlignment(Pos.CENTER);
    		textBox.setPadding(new Insets(25,0,0,0));
    		textBox.getChildren().addAll(instruction, instruction_two);
     
    		return textBox;
    	}
    	//Setting the warning to appear at the bottom of the window
    	private VBox warning() {
     
    		Text warning = new Text ("Select the radio buttons or the slider only. \nYou'll " +
    				"hear a warning sound if the mouse is clicked elsewhere!");
     
    		warning.setFont(Font.font("Calibri", 14));
    		warning.setFill(Color.RED);
    		warning.setTextAlignment(TextAlignment.CENTER);
     
    		VBox warningBox = new VBox();
    		warningBox.setAlignment(Pos.CENTER);
    		warningBox.setPadding(new Insets(10));
    		warningBox.getChildren().add(warning);
     
    		return warningBox;
    	}
     
    	//This method checks to see which button is clicked and changes the colour accordingly
    	private void processRadioButton(ActionEvent event) {
    		if (redButton.isSelected()) {
    			square.setFill(Color.RED);
    		}
    		else if(greenButton.isSelected()) {
    			square.setFill(Color.GREEN);
    		}
    		else {
    			square.setFill(Color.ORANGE);
    		}
    	}
     
    	//This method sets up the radio buttons, their labels, and positions
    	private VBox radiobuttons() {
    		ToggleGroup group = new ToggleGroup();
     
    		redButton = new RadioButton("Red");
    		redButton.setSelected(true);
    		redButton.setToggleGroup(group);
     
    		greenButton = new RadioButton("Green");
    		greenButton.setToggleGroup(group);
     
    		orangeButton = new RadioButton("Orange");
    		orangeButton.setToggleGroup(group);
     
    		redButton.setOnAction(this::processRadioButton);
    		greenButton.setOnAction(this::processRadioButton);
    		orangeButton.setOnAction(this::processRadioButton);
     
    		VBox buttons = new VBox(10);
    		buttons.setAlignment(Pos.CENTER_LEFT);
    		buttons.setPadding(new Insets(50));
    		buttons.getChildren().addAll(redButton, greenButton, orangeButton);
     
    		return buttons;
    	}
     
    	//Setting up the square's alignment
    	private HBox setShape() {
    		HBox shapeBox = new HBox();
    		shapeBox.setAlignment(Pos.CENTER);
    		shapeBox.setPadding(new Insets(0,70,0,0));
    		shapeBox.getChildren().add(square);
     
    		return shapeBox;
    	}
     
    	//Setting up the slider to control the size of the square
    	private VBox setSlider() {
    		Slider slider = new Slider(0, 100, 75);
    		slider.setShowTickMarks(true);
    		slider.setShowTickLabels(true);
     
    		square.heightProperty().bind(slider.valueProperty());
    		square.widthProperty().bind(slider.valueProperty());
     
    		VBox slide = new VBox(10);
    		slide.setAlignment(Pos.CENTER);
    		slide.setPadding(new Insets(15,0,10,0));
    		slide.getChildren().add(slider);
     
    		return slide;
    	}
     
    	//This method sets up the warning audio that sounds every time the
    	//mouse is clicked anywhere except for the radio buttons and the slider
    	private void processWarningAudio(MouseEvent event) {
    	    Object target = event.getTarget();
     
    	    // Check if the target is a RadioButton or the Slider itself
    	    if (target instanceof RadioButton || target instanceof Slider) {
    	        return; // Do nothing if the click is on a valid control
    	    }
     
    	    // Play warning sound if clicked elsewhere
    	    audio.play();
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JavaFx application not working as expected!

    Try debugging the code to see what the value of target is to determine why the if statement is failing.
    One way is to print out the value of target so you can see its value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2024
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaFx application not working as expected!

    What do you mean? How do I do that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JavaFx application not working as expected!

    Use: System.out.println("target=" + target); // show value of target

    That shows the value of target. Look at that to see if it is what you expect. Then think of how to recognize what objects you need to see to determine when to call audio. Perhaps what is shown isn't the what you expect. Maybe the actual object is the parent or child of target. Look at the API doc to see how to get to those other objects.
    Or maybe the logic is easier to call audio when the target for the clicked on object is a VBox or HBox.

    Add the print statement and click on several different places in the window. The print out will help you decide on some logic.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2024
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Re: JavaFx application not working as expected!

    I did that and its returning that I am clicking on the radio buttons, the slider or anything else on the window, but its still giving me the warning sound when clicking on the buttons or the slider. Could it have anything to with the code returning null when I printed System.out.println(getClass().getResource("audio.m p3"));

    I made sure that audio.mp3 is in the correct place, and since the audio is sounding when I'm clicking on anything in the window, that means the path is correct. Why then is System.out.println(getClass().getResource("audio.m p3")); returning null then??

    I even tried using audio.stop() which is still doing nothing..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JavaFx application not working as expected!

    its returning that I am clicking on the radio buttons, the slider or anything else on the
    What is being printed? Are any of the targets instances of what the if statement is testing for?
    What is printed when the click is not on a button?
    What is printed when the click is on the button?

    If the if statement's expression is false, then the audio is played. The tests in the if statement's expression need to be changed for what the value of target is to control calling audio.play().

    I made sure that audio.mp3 is in the correct place
    I think it is because the sound is made when the audio.play() statement is executed.

    Could it have anything to with the code returning null
    Is the file in the right folder for the class's package? The API doc says:
    the absolute name is of the following form:
    modified_package_name/name
    Where the modified_package_name is the package name of this object
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Make a simple JavaFX application into an executable
    By simon21398 in forum Java SE APIs
    Replies: 1
    Last Post: March 18th, 2023, 02:09 PM
  2. Java org.w3c.dom.Document.normalize() isn't working as expected
    By artaxerxe in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 10th, 2020, 01:21 AM
  3. Drawing in JPanel not working as expected
    By brocode in forum AWT / Java Swing
    Replies: 12
    Last Post: May 23rd, 2013, 12:44 PM
  4. InputMismatchException try catch block not working as expected
    By mikhl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 23rd, 2011, 07:02 PM
  5. Openning a file in Native Application not Working
    By wagb278 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 8th, 2011, 02:31 PM