Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPE212 – Stuff… Assignment 4 due March 23 – this Friday.

Similar presentations


Presentation on theme: "CMPE212 – Stuff… Assignment 4 due March 23 – this Friday."— Presentation transcript:

1 CMPE212 – Stuff… Assignment 4 due March 23 – this Friday.
Winter 2018 CMPE212 12/3/2018 CMPE212 – Stuff… Assignment 4 due March 23 – this Friday. Next (and last) quiz next week. See next slide. Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

2 Quiz 3 Topics Main new topics from last quiz are:
ArrayList<T> Collection Type. Generics: Generic Classes & Methods, Use of Wildcards. The Class<T> Object. Lambda Functions. Method References. GUI Design. How JavaFX works. JavaFX Containers and Layout Manager Algorithms. And older topics such as writing methods, loops, conditionals, arrays, encapsulation and inheritance are still fair game. Winter 2018 CISC124 - Prof. McLeod

3 Quiz 3, Cont. When writing code in Quiz 3, you may have difficulty getting < > to display properly. The text box may try to treat these as html tags. Code after the <, may not display at all! It is OK to use [ ] instead for < >, when typing code in a text box. So, “ArrayList<T>” can be written as “ArrayList[T]” instead. Winter 2018 CMPE212 - Prof. McLeod

4 Today Continue: Intro to JavaFX.
Start looking at Panes and Layout Managers. Winter 2018 CMPE212 - Prof. McLeod

5 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. Winter 2018 CMPE212 - Prof. McLeod

6 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”). Winter 2018 CMPE212 - Prof. McLeod

7 JavaFX Scene Graph Here is part of the existing Node hierarchy: Node
Parent Region Control Pane Labeled Winter 2018 CMPE212 - Prof. McLeod

8 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”. Winter 2018 CMPE212 - Prof. McLeod

9 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 Winter 2018 CMPE212 - Prof. McLeod

10 Aside - JavaFX API Docs These used to be a separate from the regular Java API docs, but are now part of the “whole”. Winter 2018 CMPE212 - Prof. McLeod

11 JavaFX Scene Graph, Cont.
Typically you build your GUI by first instantiating existing node objects. For example, Label myLabel = new Label(“Hello!”); In order to display the node in the window, you need to add the object you instantiated to the existing Scene Graph structure. Winter 2018 CMPE212 - Prof. McLeod

12 JavaFX Scene Graph, Cont.
In order to be able to place your nodes (or “controls”) where you want them, you add them to a Pane object. A Pane child object includes a layout manager that decides where nodes can be placed. A Pane object can be added to different type Pane objects as if it was a node itself to give you a great deal of flexibility in your layout. Finally, the root pane object becomes the root of the scene object and the entire Scene Graph that you have constructed can be displayed. Winter 2018 CMPE212 - Prof. McLeod

13 Aside – Generating Import Statements
Eclipse is great at helping you generate a missing import statement if you try to use a class that you have not yet imported. But: if you have a choice, make sure to choose the javafx one, rather than the class from javax.swing or java.awt. Winter 2018 CMPE212 - Prof. McLeod

14 Pane Objects In order to design your window you need to understand how the different Pane objects work. From the JavaFX API Tree: Winter 2018 CMPE212 - Prof. McLeod

15 Pane Objects, Cont. Won’t discuss:
DialogPane – Used as the base container for customized dialog windows. We can just use the built-in dialogs. We don’t need to design our own. PopupControl.CSSBridge – Seems to be mostly for internal use. Winter 2018 CISC124 - Prof. McLeod

16 Aside – Layout Managers
Each pane is associated with a different layout manager – an algorithm that determines where controls are placed within the pane and often their size. Provides flexibility during design and during runtime when the size of the window is changed or the window is viewed on a different OS (or even just a different monitor resolution!) than the one on which it was designed. Winter 2018 CISC124 - Prof. McLeod

17 Layout Managers – A Reference
A Nice Reference – “Working with Layouts in JavaFX”: Winter 2018 CISC124 - Prof. McLeod

18 Aside – Absolute Positioning
Used by Microsoft’s Visual Studio .NET, for example. Control positions and sizes (in pixels) are all absolute. The visual editor helps you assign default values and to line up controls. But .NET is designed to run on one OS only… You can still use absolute positioning in JavaFX, but you are giving away some convenience. Winter 2018 CISC124 - Prof. McLeod

19 BorderPane BorderPane lays out components in top, left, right, bottom, and center positions: Winter 2018 CISC124 - Prof. McLeod

20 BorderPane, Cont. For example, to add a Button to the Top position:
BorderPane bPane = new BorderPane(); Button myButton = new Button(“Click Me!”); bPane.setTop(myButton); A component adopts its preferred size if it is smaller than the available region. Center swells to occupy neighbouring positions if they are empty. Winter 2018 CISC124 - Prof. McLeod

21 BorderPane, Cont. You can add margins around a component by using an Insets object with the static .setMargin() method. You can also specify alignment within a position using a value from the Pos enum and the static .setAlignment() method. This layout is good for blocking out an entire window into sub-regions and then you use another layout in the regions of interest. Winter 2018 CISC124 - Prof. McLeod

22 BorderPane, Cont. See FXProjectBorderPane
Test control positioning, alignment and sizing. Setting individual control properties one at a time will be painful, especially when the properties are all being set to the same value… Winter 2018 CISC124 - Prof. McLeod

23 Aside – Running JavaFX Lecture Notes Code
Create a blank JavaFX project. Remove any files in the application folder. In a file browser, unzip the archive (*.zip) file to the application folder of the project. At the moment this is just Main.java and some also have application.css. Back in eclipse, use <F5> to refresh the project listing. Winter 2018 CISC124 - Prof. McLeod

24 Aside - Component Sizing
Padding Border Margin Unless you define a preferred size the calculated size is based on the size of the rendered label plus a default padding size between the label and the border. You can also change the size of the margin in code. Winter 2018 CISC124 - Prof. McLeod

25 AnchorPane Components are anchored at a specified distance from the edge of the Pane using the static methods: “value” is in pixels as a distance from the border of the pane. Why a double? Winter 2018 CISC124 - Prof. McLeod

26 AnchorPane, Cont. See FXProjectAnchorPane. Here is the interesting part: AnchorPane root = new AnchorPane(); Button leftTopButton = new Button("Left Top"); leftTopButton.setFont(new Font("Consolas", 20)); Button rightBottomButton = new Button("Right Bottom"); rightBottomButton.setFont(new Font("System", 18)); Label leftBottomLabel = new Label("Left Bottom"); leftBottomLabel.setFont(new Font("Times New Roman", 22)); AnchorPane.setLeftAnchor(leftTopButton, 10.0); AnchorPane.setTopAnchor(leftTopButton, 10.0); AnchorPane.setRightAnchor(rightBottomButton, 10.0); AnchorPane.setTopAnchor(rightBottomButton, 10.0); AnchorPane.setBottomAnchor(rightBottomButton, 10.0); AnchorPane.setLeftAnchor(leftBottomLabel, 80.0); AnchorPane.setBottomAnchor(leftBottomLabel, 80.0); root.getChildren().addAll(leftTopButton, leftBottomLabel, rightBottomButton);

27 AnchorPane, Cont. Note the effect of window re-sizing.
Note how two or more anchors can combine to change the size of the control away from its calculated/preferred size. This is another relatively crude layout manager that might be best used to lay panes out in a window. Winter 2018 CISC124 - Prof. McLeod

28 Aside – Pane “Children”
Nodes added to a pane are also called “Children”. Add nodes to a pane by invoking the .add() or .addAll() methods on the children collection owned by the pane. The .addAll() method can also accept a Collection<? extends Node> object. The .getChildren() method of the pane object “exposes” its children collection. Winter 2018 CISC124 - Prof. McLeod

29 FlowPane Piles children into the pane in the order in which they are added from left to right (by default). The children wrap around if the available space is full. See FXProjectFlowPane. See what happens when you re-size the window. Play with gaps, padding, default sizes and alignment. Note the use of an ArrayList<Button> to hold buttons. Why are some buttons larger than others? Winter 2018 CISC124 - Prof. McLeod


Download ppt "CMPE212 – Stuff… Assignment 4 due March 23 – this Friday."

Similar presentations


Ads by Google