Question

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 citys 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.

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
I was wondering how do I add an event to the buttons for show an image...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The JavaFX framework provides more than one way to handle events. For event handlers, we could...

    The JavaFX framework provides more than one way to handle events. For event handlers, we could use inner classes, anonymous inner classes, or the new Java 8 feature of lambda expressions. In this discussion, you will explore these different ways of writing event handles in JavaFX. To prepare for this discussion, you must unzip the attached NetBeans project zip file (U4D1_HandleEvents.zip) and load it into your NetBeans IDE. The project uses an inner class to handle the click event on...

  • Answer questions 1-20 about creating GUI applications with JavaFX 1. A tree-like data structure that contains...

    Answer questions 1-20 about creating GUI applications with JavaFX 1. A tree-like data structure that contains the components of a JavaFX GUI is called a: a. directory tree b. node tree c.node graph d. scene graph 2. A node in a scene graph that contains other nodes is called a: a. root node b. branch node c. leaf node d. terminal node 3. A node in a scene graph that has no children is called a: a. root node b....

  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

  • MyMenuFrame.java, MyMenuFrameTest.java, a. The title of the frame is “MyNotepad”. b. Create and add border layout....

    MyMenuFrame.java, MyMenuFrameTest.java, a. The title of the frame is “MyNotepad”. b. Create and add border layout. c. Create a text area to display contents. Add the text area to the center of the border layout. d. Create a menu bar. e. Create a file menu. Set mnemonic for file menu. It is “F”. File menu includes three menu items. f. Add a separator between each menu item in the file menu. i. Open    1. Add a short cut for...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

  • I mainly need help with the “Mouse Events” & “Command Buttons” sections Sqrt xA2 Cir CircleButton...

    I mainly need help with the “Mouse Events” & “Command Buttons” sections Sqrt xA2 Cir CircleButton Clas:s The graphic circular buttons are created by drawing a filled Circle on a StackPane. So, the pictured GUI uses 9 different StackPanes tor displaying the 9 qraphic buttons. Of course, these CircleButton objects can then be placed on a single GridPane lo achieve the 3x3 layoul (see SimpleCalc class below). Creale a class narned CircleBullon thal exlends the StackPane class. The class should...

  • I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline...

    I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline (10 points). Create an outline in comments/psuedocode for the programming assignment below. Place your comments in the appropriate files: main.cpp, functions.h, dealer.cpp, dealer.h, dealer.cpp (as noted below). Place into a file folder named LastnamePA3, the zip the content and hand in a zip file to Canvas. Part II: PA3: Car Dealership (40 points) For Programming Assignment 3 you will be creating a program to...

  • I need some help i need to do this in C# Objectives: • Create an application...

    I need some help i need to do this in C# Objectives: • Create an application that uses a dictionary collection to store information about an object. • Understanding of abstract classes and how to use them • Utilize override with an abstract class • Understanding of Interfaces and how to use them • Implement an Interface to create a “contract” between classes. • Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the...

  • Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses:...

    Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses: Exception handling File Processing(text) Regular Expressions Prep Readings: Absolute Java, chapters 1 - 9 and Regular Expression in Java Project Overview: Create a Java program that allows a user to pick a cell phone and cell phone package and shows the cost. Inthis program the design is left up to the programmer however good object oriented design is required.    Project Requirements Develop a text...

  • Java FX Application Purpose The purpose of this assignment is to get you familiar with the...

    Java FX Application Purpose The purpose of this assignment is to get you familiar with the basics of the JavaFX GUI interface components. This assignment will cover Labels, Fonts, Basic Images, and Layouts. You will use a StackPane and a BorderPane to construct the layout to the right. In addition you will use the Random class to randomly load an image when the application loads Introduction The application sets the root layout to a BorderPane. The BorderPane can be divided...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT