Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I was wondering how do I add an event to the buttons for show an image on the me

ID: 3752864 • Letter: I

Question

I was wondering how do I add an event to the buttons for show an image on the menu bar by using lamda? 5C

Create a JavaFX project in Eclipse with a name like ex2 or exercise2. Use the default package application. If you want, you may refactor it. Use the default JavaFX class (a source file) named Main.java. Again, you may change the name of the source file (class file, eventually) by refactoring. Download the runnable JAR file named Gratitude-Jar from Exercise #1 to your computer and double click to run it. Click a button to see the expression of gratitude in a different language block. Create a menu bar in the Main.java with Application, Gratitude, and Viewlmage menus. You may use any layout pane. 1. 2. 3. 4. 5. The Application menu has Exit menu item to exit the application. Use anonymous class for event handling. a. b. The Gratitude menu has English, Spanish, French (or any 3 languages) menu items. Use lambda expressions for event handling. Clicking a language prints a greeting. The Viewlmage menu has three city names as menu items (e.g., Duluth, Atlanta, and London). When you click a city, the city's picture appears. Use Image and ImageView controls (See this or else on the web). Use the alternative lambda expressions (e.g., lambda expressions with separate methods) for event handling c. 6. 7. Export your project to a JAR file (not a runnable JAR file). Upload the JAR file on Canvas.

Explanation / Answer

Hello, since you haven’t provided the code for this program, all I can do is provide a solution based on assumptions. Assuming you have three buttons in ViewImage menu, named city1,city2,city3. And you have loaded images for the three cities in three Image objects, you also created an ImageView, and added to the pane somewhere. Something similar to the below set of code should be there in your program.

   //Assuming these are the images in ViewImage menu

    Button city1, city2, city3;

    //loading images of cities, use your file path, dont remove prefix 'file:'

    Image city1Image = new Image("file:images/city1.jpg");

    Image city2Image = new Image("file:images/cit2.jpg");

    Image city3Image = new Image("file:images/city3.jpg");

    //assuming this is the imageview wheres we display the city image. Assuming

    //that it is added to the pane somewhere.

    ImageView cityImageView = new ImageView();

Now we have to add event handlers for the buttons using lambda expression. Use below code for this.

//adding event listeners to the buttons on the ViewImage menu

              //using a lambda expression, and calling showCityImage() passing values

              //either 1,2 or 3

              city1.setOnAction(e -> showCityImage(1));

              city2.setOnAction(e -> showCityImage(2));

              city3.setOnAction(e -> showCityImage(3));

Finally, the below function will handle the button clicks, and display appropriate image in the image view.

//method to display image of a city on the imageview

    public void showCityImage(int i) {

        //updating image of cityImageView imageview based on the value of i

        if (i == 1) {

            //first city

            cityImageView.setImage(city1Image);

        } else if (i == 2) {

            //second city

            cityImageView.setImage(city2Image);

        } else if (i == 3) {

            //third city

            cityImageView.setImage(city3Image);

        }

    }

If you have any doubts, just drop a comment. Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote