Learning Journal Unit 7 PDF

Title Learning Journal Unit 7
Author Chegg Lee
Course Programming 1
Institution University of the People
Pages 5
File Size 159.9 KB
File Type PDF
Total Downloads 36
Total Views 137

Summary

unit 7 learning journal java...


Description

This week learning journal introduces us to a very critical aspect of Java programming which is GUI programming. As noted earlier by (Eck. D, 2019, pp.267), most users find interacting with the computer in command line or through a terminal rather “alien”. Users generally prefer a user interface that is graphical and allows them to click on buttons, see labels, enter input using a keyboard and thins like that. Fortunately, Java provides coding toolkits or packages that make GUI programming possible through one’s such as JavaFX and swing.

The chapter focuses on the use of JavaFX too illustrate GUI Java programming. The main point to note is the need for windows in a GUI application like the one’s we see on a Windows or Linux system. These windows, in code are represented by what is called a Scene. This scene is where the GUI components are contained. In a nutshell, a scene is a container for the components such as buttons and labels. I, personally choose to use IntelliJ as my IDE mainly for the reason that when I start a JavaFX project, all the necessary imports are done automatically. This is particularly important to me because there are a lot of imports that are crucial in GUI programming. A GUI class/program has an important start() method that accepts a Stage parameter. This scene is contained within a window or the Stage object.

All components, whether they are basic components or containers belong to sub-classes of the javafx.scene.Node. The Node class has other sub-classes that handle the components according to their types. For example, the Parent sub-class (javafx.scene.Parent) of the Node class provide for the container components. Container components have a “Layout” arrangement I which they arrange their components. An example given by (Eck. D, 2019) of a container is an

HBox that “arranges the components that it contains in a horizontal row.” There are many other examples of predefined layouts such as “HBox, VBox, Border Pane, Stack Pane, Text Flow, Anchor Pane, Title Pane, Grid Pane, Flow Panel, etc.”, as listed by (JavaFX – Layout Panes(Containers) – tutorialspoint, n.d).

In this learning week, we also look into a very important topic of GUI programming which is the event handling. This is also made possible by the start() method of the GUI program. Any event that is to be handled is the response of the program to actions performed by the user such as clicking a button or a mouse key. The program should be able to handle these actions and provide the needed output through the event handler. An important import for event handling is the java.awt.event.ActionListner. A listener is responsible for waiting for the user to perform an action such as clicking a button. When this happens the code subsequently handles that action. Below is an example of code that handles the clicking of buttons. The below code is a slight modification of code provided by (thenewboston, 2009), in his Youtube lecture ->

package sample; import import import import import

javafx.application.Application; javafx.fxml.FXMLLoader; javafx.scene.Parent; javafx.scene.Scene; javafx.stage.Stage;

import import import import import import import import

java.awt.*; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.net.PasswordAuthentication; javax.swing.JFrame; javax.swing.JTextField; javax.swing.JPasswordField; javax.swing.JOptionPane;

public class Main extends Application {

public void start(Stage primaryStage) extends JFrame{ private JTextField item1 = new JTextField(); private JTextField item2 = new JTextField(); private JTextField item3 = new JTextField(); private JPasswordField passwordField = new JPasswordField(); public Main() //this is the constructor { super("First app"); setLayout(new FlowLayout()); item1 = new JTextField(10); add(item1); item1 = new JTextField("Enter text here"); add(item1); item1 = new JTextField("uneditable", 20); item3.setEditable(false); add(item1); passwordField = new JPasswordField("mypass"); add(passwordField); /* * create an ActionLister called handler1 */ thehandler handler1 = new thehandler(); /*Add the above ActionLister to all the components of the GUI window*/ item1.addActionListener(handler);

item2.addActionListener(handler); item3.addActionListener(handler); passwordField.addActionListener(handler); } private class thehandler implements ActionListener //this class has been added to components above to handle events { public void actionPerformed(ActionEvent event) { String string = ""; if (event.getSource() == item1) //if the event occurred in item1 e.g click a button string = String.format("field1: %s", event.getActionCommand()); else if (event.getSource() == item12) string = String.format("field2: %s", event.getActionCommand()); else if (event.getSource() == item3) string = String.format("field3: %s", event.getActionCommand()); else if (event.getSource() == passwordField) string = String.format("password field is : %s", event.getActionCommand()); JOptionPane.showMessageDialog(null, string); //pops up a message dialogue } } }

public static void main(String[] args) { launch(args); } }

The handler class does the event handling for the user by implementing the ActionListener as illustrated above.

References

Eck, D. J. (2019). Introduction to programming using Java, version 8.1. http://math.hws.edu/javanotes

JavaFX - layout Panes(Containers) - tutorialspoint. (n.d.). Tutorialspoint.Com. Retrieved May 19, 2021, from https://www.tutorialspoint.com/javafx/javafx_layout_panes.htm#: %7E:text=JavaFX%20provides%20several%20predefined%20layouts,belongs%20to %20the%20package%20javafx.

thenewboston. (2009, September 20). Java programming tutorial - 53 - ActionListner [Video]. YouTube. https://www.youtube.com/watch?v=qhYook53olE...


Similar Free PDFs