Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC124 Last Quiz next week. Topics listed in Tuesday’s lecture.

Similar presentations


Presentation on theme: "CISC124 Last Quiz next week. Topics listed in Tuesday’s lecture."— Presentation transcript:

1 CISC124 Last Quiz next week. Topics listed in Tuesday’s lecture.
Fall 2018 CISC124 12/31/2018 CISC124 Last Quiz next week. Topics listed in Tuesday’s lecture. Last assignment due last day of class. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

2 Today Finish Lambda Functions. Method References.
Event-Driven Programming. Fall 2018 CISC124 - Prof. McLeod

3 Lambda Functions, Cont. Filter is an example of a Functional Interface. These interfaces can only contain a single abstract method. Lambdas can only be created using Functional Interfaces. You can use annotation to make sure your interface is OK. Fall 2018 CISC124 - Prof. McLeod

4 Pre-Defined Functional Interfaces
Turns out the java.util.function package has many pre-defined generic functional interfaces. See the API, svp. The one that matches our check function signature is called Predicate<T>. It has the abstract method signature: boolean test(T) See TestFilter4.java. Even better than the last best one! Fall 2018 CISC124 - Prof. McLeod

5 Method References ArrayList.sort() accepts a Comparator<T> object that can specify the desired algorithm for comparison of objects of type T. Turns out Comparator is a Functional Interface, so you can build a lambda function for a comparator. See TestSorting.java. Fall 2018 CISC124 - Prof. McLeod

6 Method References, Cont.
But suppose our Person class has a method that matches the Comparator interface: public static int compareByAge(Person p1, Person p2) { return p1.age - p2.age; } In this case you can supply a Method Reference instead of building the lambda function. See TestSortingAgain.java. Fall 2018 CISC124 - Prof. McLeod

7 Method References, Cont.
db.sort(Person::compareByAge); Wow, that was easy! Even easier than a Lambda! The method reference must match the functional interface’s specification. You can use non-static methods or methods from an instance. If you wish to supply a constructor, use: ClassName::new And you can use existing API methods! Fall 2018 CISC124 - Prof. McLeod

8 Lambda Functions, Cont. You can have multiple lines of code in a lambda, but don’t make them too long. You can use as many lambdas as you wish when invoking a function, as long as they each match up to some functional interface. In GUI programming the most common event handler interface, EventHandler<ActionEvent> is a functional interface, so lambdas can be used to attach event code to handlers. Fall 2018 CISC124 - Prof. McLeod

9 Event-Driven Programming
Fall 2013 CISC124 Event-Driven Programming So far, for assignments, you have written single threaded “batch” programs – the coder (you!) controls the flow of execution. For event-driven programs, the user controls the flow by initiating events. A GUI interface consists of components contained within a frame (or “window”). Components and even the frame itself can respond to events. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

10 Possible Events Some things that can trigger code execution:
Left mouse click down on a command button. Left mouse click down on any component. Similarly for any mouse button. Similarly for holding a mouse button down. Similarly for mouse button up. Double click on a component… Cursor over a component. Cursor leaving a component. Cursor hovering over a component. Component has focus or has lost focus. Component contents have changed. Alt-key or Cntrl-key or Shift-key, etc… Fall 2018 CISC124 - Prof. McLeod

11 Events, Cont. Most events you ignore – your interface does not have to respond to every possible keyboard and mouse initiated event – that would be crazy!! To respond to an event in code, you attach an Event Handler object to a component. When an event occurs to a component that has the focus, the handler receives an object which contains information about the source of the event (which mouse button, etc.) You decide which events are of interest and what you want your program to do when these events occur. Fall 2018 CISC124 - Prof. McLeod

12 GUI Construction Construction of a Graphical User Interface involves:
Fall 2013 CISC124 GUI Construction Construction of a Graphical User Interface involves: Creating the design of the window – choosing components (buttons, labels, text boxes, etc.) and where they are positioned in the window. Changing properties, including appearance properties, of the components. Adding and linking functionality to events, where needed. Repeating these steps for each window! Connecting the program flow between windows. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

13 History of GUI Components in Java
Fall 2013 CISC124 History of GUI Components in Java Next slide has a hierarchy of Historical Interest: These classes are part of the Component hierarchy in javax.swing Swing was a modern improvement of the older AWT (“Abstract Windows Toolkit”) classes. The following diagram is a simplified summary of the structure: Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

14 java.awt javax.swing Abstract Class Concrete Class Object Component
BorderLayout FlowLayout GridLayout Container Window java.awt Frame javax.swing JFrame JComponent Fall 2018 CISC124 - Prof. McLeod

15 javax.swing JFrame JComponent JPanel AbstractButton JLabel JMenuBar
JTextComponent JMenuItem JButton JMenu JTextArea JTextField Fall 2018 CISC124 - Prof. McLeod

16 JavaFX JavaFX classes are no longer part of this hierarchy.
JavaFX is a bold, relatively new world of GUI coding! Swing API code will no longer be updated. Any improvements will be in JavaFX packages only. Library code used to be in a separate API, but now javafx.* packages are included in the distributed JRE (in various javafx.* modules). You still need to configure eclipse to program in JavaFX (later). Fall 2018 CISC124 - Prof. McLeod

17 JavaFX, Cont. Swing was developed mostly for enterprise/business use, not for personal use and certainly not for mobile devices. JavaFX is available in the API since Java 7. The latest is called JavaFX 10. Jazzier controls and more support for portable devices. Fall 2018 CISC124 - Prof. McLeod

18 “Client Technologies”
For lots of links on JavaFX and Swing see: Fall 2018 CISC124 - Prof. McLeod

19 Getting Started Using JavaFX - Topics
Overview / Installation. Start by looking at the Scene/Stage model. What is a Scene Graph? How components are laid out using Pane-based layout managers. Look at the use of CSS style sheets. Attach events to components. Look at the use of FXML files and SceneBuilder. Fall 2018 CISC124 - Prof. McLeod

20 JavaFX Overview You can separate style and layout from functional code using *.css and *.fxml files. Very modern! Contains a rich set of tools for: Web browsing. Media playing. 2D and 3D drawing and animation. Including many visual effects. Taking advantage of hardware accelerated rendering. Fall 2018 CISC124 - Prof. McLeod

21 JavaFX Overview, Cont. Already contains separate threads for:
The main application. Rendering. Media playback. Over 50 controls, built-in. A dozen built-in chart types. 2D and 3D transformations as well as many other visual effects can be applied to most controls. Fall 2018 CISC124 - Prof. McLeod

22 Fall 2013 CISC124 JavaFX - Installation You need to add e(fx)clipse to eclipse in order to have the libraries and tools you need to write JavaFX code. You don’t need SceneBuilder but might like to play with it later. See the Resources page on the course web site. The homepage for e(fx)clipse is: Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

23 e(fx)clipse Version May 2018: released version 3.3.0...
Follow the instructions on the course “Resources” page. Fall 2018 CISC124 - Prof. McLeod

24 SceneBuilder Version Latest installer build linked to the Gluon site is (June, 2018) for Java 10. A drag and drop visual editor that edits an *.fxml file in the background. SceneBuilder can be invoked from within eclipse, but essentially runs outside of eclipse. More later… Fall 2018 CISC124 - Prof. McLeod

25 New JavaFX Project Wizard
Sets up quite an elaborate structure for you! Don’t have more than one JavaFX project per eclipse project. Let’s have a look! Fall 2018 CISC124 - Prof. McLeod

26 New JavaFX Project Wizard, Cont.
Your GUI class needs to extend the javafx.application.Application class. Your main() method must invoke the inherited launch() method to start the application. Supply the args from the main parameter list to launch(). The wizard overrides the start() method from Application and the parameter for start() is the Stage object for the window, called “primaryStage”. Fall 2018 CISC124 - Prof. McLeod

27 New JavaFX Project Wizard, Cont.
An item (layouts, controls, images, etc.) is called a “node”. Nodes are added to the Nodes hierarchy (next, next slide), and the root node of this hierarchy is added to the Scene object when it is instantiated (and it is given a size in pixels). The Scene object is then put on the stage using the .setScene() mutator. When the scene is all constructed the “curtains are raised” by invoking .show() on the stage object. Fall 2018 CISC124 - Prof. McLeod

28 New JavaFX Project Wizard, Cont.
The wizard also adds a link to a CSS (“Cascading Style Sheet”) file for the scene, called “application.css”. Initially the file is empty, but you can “skin” the scene using css style specifications for the window and any controls you are using. You can do all this in your GUI class too, but using the css file allows you to separate style code from application code. We’ll look into doing this a bit later. For now we’ll just use the default style (“Modena”). Fall 2018 CISC124 - Prof. McLeod

29 JavaFX Scene Graph Here is part of the existing Node hierarchy: Node
Parent Region Control Pane Labeled Fall 2018 CISC124 - Prof. McLeod

30 JavaFX Scene Graph, Cont.
Left-hand branch are containers – used to contain and organize other controls. Right-hand branch are the controls. Usually a container type object will be the “root”. Fall 2018 CISC124 - Prof. McLeod

31 JavaFX Scene Graph, Cont.
There are many more classes extending nodes in this hierarchy. For example, some classes extending from Labeled: Labeled ButtonBase Label Button CheckBox ToggleButton See the JavaFX API Tree diagrams for all the classes. RadioButton Fall 2018 CISC124 - Prof. McLeod

32 Aside - JavaFX API Docs Are now included in the normal set of API docs that you can download. Let’s have a look! Fall 2018 CISC124 - Prof. McLeod


Download ppt "CISC124 Last Quiz next week. Topics listed in Tuesday’s lecture."

Similar presentations


Ads by Google