Question: JAVA SOLUTION This lab has four parts: Create a window. Create 5 buttons in that window. Create an event when a button is pushed. Create

JAVA SOLUTION

This lab has four parts:

  1. Create a window.
  2. Create 5 buttons in that window.
  3. Create an event when a button is pushed.
  4. Create an event listener that will respond to the button being pushed event.

Task 1 Create a Window

For Java:

  1. Please ensure that the following import statements are used:

import javafx.application.Application;

import javafx.stage.Stage;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.GridPane;

import javafx.scene.text.Text;

import javafx.event.EventHandler;

import javafx.scene.input.MouseEvent;

  1. Next, ensure that there is a main method which is simply using the launch method.
  2. Ensure there is a start method as well. Erase all code inside of it.
  3. Create a new GridPane object using the default constructor. Well use that in the next task, but we need it for the following set up commands.
  4. Create a new Scene object passing in the GridPane object, the width of the scene and the height of the scene.

NOTE: Scenes are similar to the content of a tab (a webpage) in a modern internet browser. Where stages are similar to the browser itself, which can move between webpages, but only ever shows one.

  1. Using the primaryStage object (which comes from the parameter of the start method), call the setScene method and pass in the Scene we just made.
  2. Finally, use primaryStages show method to display the window. You should be able to run it and see a blank white window appear.

Task 2 Create Buttons

We can make many types of interact-able objects in our window, but we will keep it simple and use a clickable button.

For Java:

  1. We must first create a button object using the Button class. Please use the overloaded constructor and pass in the text that will be displayed on the button. Something simple, like button 1 would be fine as long as the number changes for each new button.
  2. Now we must add it to our window. This is where the GridPane object comes in handy. Use the GridPane objects add method, passing in the button, row position, and column position.

NOTE: The GridPane is applied to the Scene (our window/page). This enforces a grid structure on our window, which allows us to move our items around on the grid and thus window. Its helpful for keeping even spacing between all of our items while letting us easily move buttons through the row and column position (and not pixel specific locations). The grid starts at (0, 0) in the top left of the window.

  1. You should be able to see the button in the window now. You may need to make your window bigger if you cannot see it (anything at position (0, 0) should almost always be visible).
  2. Make sure to add the remaining buttons using the same process. Dont put them in the same grid positions though, or you wont be able to see all of them.

Task 3 Create an Event

At this point, we will want to create an event object every time the button is pushed. This way we will be able to tell when the button is pushed and what to do when it is pushed.

For Java:

  1. For this part, we will need to create a new EventHandler object. This is a parameterized class (this uses the <> brackets), which means that we will need to give it some specific type of event to handle when we declare a new EventHandler. We will simply use the MouseEvent class since we want the buttons to be clicked. Also we can simply use the default constructor.

EventHandler eventHandler = new EventHandler();

  1. Youll notice that we cannot instantiate an EventHandler. So we will create a way of doing so using what is called in-line notation. At the same time, we will also have to implement the inherited abstract method called handle. However, for now we will leave it empty.

//Creating the mouse event handler

EventHandler eventHandler = new EventHandler()

{

@Override

public void handle(MouseEvent e)

{

}

};

  1. At this point, it should be compliable and so we have completed making an EventHandler object.

Task 4 Create an Event Listener

Lastly, we will create an event listener to handle the events (buttons being pushed) and display a message, on the GUI, saying which number button was pushed.

For Java:

  1. First we will need to create a Text object. Use the overloaded constructor and pass in the string that should be displayed (similar to the button). Also make sure to add it to the GridPane.
  2. Then we attach the EventHandler to a button using the Button classs addEventFilter method that takes in an event (in this case MouseEvent.MOUSE_CLICKED) and the EventHandler we made earlier.
  3. Finally using the handle method that we left blank earlier, add some code to it so that the method is changing the text to display which button was clicked on. (This may require more than one EventHandler).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!