• No results found

GraphicComponents

N/A
N/A
Protected

Academic year: 2020

Share "GraphicComponents"

Copied!
108
0
0

Loading.... (view fulltext now)

Full text

(1)

JavaFX Components

(2)

JavaFX

Graphics packages

• Combines old awt and swing libraries

• Can run in web browsers

(3)

Basics

Class must extend Application class

• Class must override and implement the start method

All components attached to the main stage

(4)

Example – SceneExample.java

Example – SceneExample.java

(5)

Application.launch(args)

Launches stand-alone JavaFX applications • Similar to main method

String array as arguments

Automatically called by JVM

Do not need main method

When running from command line

JVM automatically calls launch for youShould always have main method anyway

(6)

Start Method

Will be called when the application is

launched

• Has a stage as a parameter

This is where all your components will eventually

be attached to

(7)

JavaFX Setup

Theater analogy

Stage = the window you will see

– Everything attached to it

Scene

– Node parameter = what will be attached to the scene

• Usually a layout type to build more complex layouts from

Width and height are optional and will be set by

default

(8)

Scene Graph

Hierarchical tree of nodes for all visual

elements

• Applying transformations to a parent node can also apply to the child nodes

• https://

(9)

Node

• A node is any component or layout

• Nodes are attached to other nodes

• Nodes are attached to scenes

Examples

Buttons

CheckboxesPanels

• Which can contain more nodes

(10)

setScene

Method of stage class

• Sets the scene to be displayed

• Can use this to swap scenes when using multiple scenes (cool!)

(11)

show

Method of stage class • Displays the scene

• Don’t forget to call this once scene is setup

(12)

sizeToScene

Method of Window class

• Sizes the stage (window) to the size of the scene it contains

Basically an auto-size feature

(13)

setTitle

Method of stage class

• Sets the title of the window

The text at the top bar of the window

mainFrame.setTitle("Title of Window");

(14)

setTitle

(15)

Summary so far

Stage = window

http://

docs.oracle.com/javase/8/javafx/api/javafx/stage/St age.html

Stage contains one scene at a timeScene = place to add nodes to

http://

docs.oracle.com/javafx/2/api/javafx/scene/package -summary.html

Node = any GUI component or panel

http://

docs.oracle.com/javafx/2/api/javafx/scene/Node.ht ml

(16)

Components

ButtonTextFieldTextAreaLabelRadioButtonCheckBoxComboBox

Image + ImageViewScrollPane

(17)

Methods

Several Components Use The Same Methods

(18)

Button

Button class for the buttons

Makes sense…

Typically constructed with the button text

Button button = new Button(“Button Text”);

Does nothing by default

Have implement button handlers separately

http://

docs.oracle.com/javase/8/javafx/api/javafx/sce ne/control/Button.html

(19)

Example – ButtonExample.java

• ButtonExample.java

• Example corresponds to the following slides on buttons

(20)

setText

Sets the text of a component

In this case a button

May need to call sizeToScene

The text changes the size of the button

The change in size might mess up formatting

(21)

setText: Before Setting Text

(22)

setText: After Setting Text

(23)

setText: After sizeToScene

(24)

setTextFill

Sets the paint of the button text • Typically a color is used

Color is a sub class of Paint

(25)

Paint class

• Abstract class

(26)

Color class

Provides several constants for colors to use • Can make your own color object

Red, Green, Blue, Opacity

• Can play with brightness and saturation

• http://

docs.oracle.com/javafx/2/api/javafx/scene/pain t/Color.html

(27)

setTextFill

button2.setTextFill(Color.BLUE);

(28)

setTextFill

button2.setTextFill(new Color(1, 0, 0, 1));

red, green, blue, opacity – Range = 0-1

Red = 1 = full red, no green or blue – Opacity = 1 = can see it!

– Opacity = 0 = see through / transparent

(29)

.setPrefWidth

Sets the width of the component

• This width will usually be displayed

• May need to call sizeToScene

(30)

.setPrefWidth

button3.setPrefWidth(200);

(31)

.setPrefHeight

Sets the height of the component

• This height will usually be displayed

• May need to call sizeToScene

(32)

.setPrefHeight

button3.setPrefHeight(200);

(33)

setMaxWidth

Sets the maximum width for the component

In this case the button

The component will never be resized larger

than the maximum width

• Typically set all components’ max width to Double.MAX_VALUE

This allows them to resize correctly

(34)

.setMaxWidth

No max width set for button 1

(35)

.setMaxWidth

button1.setMaxWidth(Double.MAX_VALUE)

;

(36)

setMaxHeight

Sets the maximum height for the component

In this case the button

The component will never be resized larger

than the maximum height

• Typically set all components’ max height to Double.MAX_VALUE

This allows them to resize correctly

(37)

.setMaxHeight

No max height set for button 6

(38)

.setMaxHeight

• button6.setMaxHeight(Double.MAX_VALUE);

(39)

Sizes

setMaxWidth = Component will never have width larger than min width

setMaxHeight = Component will never have height larger than min height

setPrefWidth = typical width of componentsetPrefHeight = typical height of component

setMinWidth = Component will never have width smaller than min width

setMinHeight = Component will never have height smaller than min height

(40)

.setFont

Sets the font of the button text

Type and size

Uses the Font class to choose fonts from

• May have to call sizeToScene after changing font

(41)

.setFont

button4.setFont(Font.font("Times New Roman", 20));

(42)

.setFont

button4.setFont(Font.font

("Times New Roman", FontWeight.BOLD, 20));

(43)

.setFont

button4.setFont(Font.font

("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 20));

(44)
(45)

.setBorder

Sets the border of the component • Need to create a Border

• Need to create a BorderStroke first

(46)

BorderStroke

Type of border to display

• Construct with a color, style, corner type, and stroke weight

http://

docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Borde rStroke.html

(47)

Border

The border of the component

• Constructed with a border stroke

• http://

docs.oracle.com/javase/8/javafx/api/javafx/sce ne/layout/Border.html

(48)

Border Example 1

BorderStroke borderStroke1 = new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY,

BorderWidths.DEFAULT);

Border border1 = new Border(borderStroke1); button5.setBorder(border1);

mainFrame.sizeToScene();

(49)

Border Example 1

(50)

Border Example 2

BorderStroke borderStroke1 = new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new

BorderWidths(5));

Border border1 = new Border(borderStroke1); button5.setBorder(border1);

mainFrame.sizeToScene();

(51)

Border Example 2

(52)

Border Example 3

BorderStroke borderStroke1 = new BorderStroke(Color.RED, BorderStrokeStyle.DASHED, CornerRadii.EMPTY, new

BorderWidths(5));

Border border1 = new Border(borderStroke1); button5.setBorder(border1);

mainFrame.sizeToScene();

(53)

Border Example 3

(54)

Alignment

Can set position of the component if it is not set

to fill it’s entire space

– Horizontal position

• Center

LeftRight

– Vertical position

• Top

• Center

Bottom

(55)

Alignment

Set preferred height for button3 to 200 • Set preferred width for button5 to 200

(56)

Alignment

Default alignment for button6

(57)

Alignment

button6.setAlignment(Pos.CENTER);

(58)

Alignment

button6.setAlignment(Pos.TOP_LEFT);

(59)

Alignment

button6.setAlignment(Pos.BOTTOM_RIGHT)

;

(60)

Alignment

Pos enum in javafx.geometry class • http://

docs.oracle.com/javafx/2/api/javafx/geometry/ Pos.html

(61)

Graphic

Set the graphic of a button to an image • Uses the Image and ImageView class

• Can also set the location of the graphic relative to the button text

(62)

Image + ImageView Class

Load an image from a file

Image

Directly stores the image

ImageView

Can manipulate the image

• Rotate

ScaleEtc…

(63)

Image + ImageView Class

• Image – http://

docs.oracle.com/javafx/2/api/javafx/scene/image/I mage.html

• ImageView – http://

docs.oracle.com/javafx/2/api/javafx/scene/image/I mageView.html

(64)

Button Image

Image image = new Image("testImage.png"); ImageView imageView = new

ImageView(image);

button6.setPrefHeight(200); button6.setPrefWidth(200);

button6.setGraphic(imageView); mainFrame.sizeToScene();

(65)

Button Image

(66)

Button Image

button6.setContentDisplay(ContentDisplay.TOP);

(67)

Buttons

Can do much more than mentioned • I only showed the basics

• See javadocs for more stuff

Don’t forget to look at all inherited methods

Most of the methods we use are not in the button

class, they are inherited from other classes

• http://

docs.oracle.com/javase/8/javafx/api/javafx/sce ne/control/Button.html

(68)

TextField

Standard text field you see everywhere • Where the user types in things

• One line

TextArea = multiline

• Not scrollable by default

• http://

docs.oracle.com/javase/8/javafx/api/javafx/sce ne/control/TextField.html

(69)

Example – TextFieldExample.java

TextFieldExample.java

• Example corresponds to the following slides on text fields

(70)

TextField Image

(71)

setFont

• textField.setFont(Font.font("Arial Black", 20));

– TextField got bigger to fit the text

(72)

.setAlignment

textField.setAlignment(Pos.CENTER);

(73)

.setText and .getText

textField.setText("LOWER CASE");

textField.setText(textField.getText().toLowerCase());

(74)

.setVisible

Can set components to invisible

They will not be seen

Useful when certain users should not see

certain components

• Useful when certain options are not yet available and should be invisible

• Ex: textField.setVisible(false);

(75)

.setDisable

Enables the component

Default = enabled

Disabled

TextField = cannot type into and text is grayed out

Useful for an output text field

Buttons = cannot click and grayed out Ex: textField.setDisable(true);

(76)

TextArea

For multi-row text • Scrollable by default

• Not much different than a TextField

• http://

docs.oracle.com/javase/8/javafx/api/javafx/sce ne/control/TextArea.html

(77)

Example – TextAreaExample.java

Example – TextAreaExample.java

• Example corresponds to the following slides on text areas

(78)

TextArea

@Override

public void start(Stage mainFrame) {

TextArea textArea = new TextArea(); scene = new Scene(textArea);

mainFrame.setScene(scene); mainFrame.show();

}

(79)

TextArea

(80)

.setWrapText

Sets whether the text will wrap or not • textArea.setWrapText(false);

• textArea.setWrapText(true);

(81)

.setPrefRowCount

.setPrefColumnCount

• Basically setting the preferred side of the TextArea

.setPrefRowCount

Number of rows to display

.setPrefColumnCount

Number of columns to display

Example:

textArea.setPrefRowCount(5);

textArea.setPrefColumnCount(20);

(82)

Label

Basically a disabled TextField

• Used as labels that are not meant to be edited

• Can be images

• Uses most methods buttons do

• Label label = new Label("THIS IS A LABEL");

(83)

Example – LabelExample.java

Example – LabelExample.java

(84)

CheckBox

Standard CheckBoxes you typically see

• Can be set to selected by default

• http://

docs.oracle.com/javase/8/javafx/api/javafx/scen e/control/CheckBox.html

• CheckBox checkBox = new CheckBox("Name of Box");

(85)

Example – CheckBoxExample.java

CheckBoxExample.java

(86)

.setSelected

checkBox.setSelected(true);

(87)

Other Useful Methods

.setFont = can set font of check box text

• .isSelected = can set it to be selected or not

• .setDisable = can disable the CheckBox

• .setText = can set the text of the CheckBox

• .setGraphic = can set the image of the CheckBox

(88)

RadioButton

Standard radio button you typically seeUsually in groups of two or more

Usually mutually exclusive

• Only one can be selected at a time

By default they are not mutually exclusiveNeed to add them to a ToggleGroup

• Mutually exclusive within the ToggleGroup

http://

docs.oracle.com/javase/8/javafx/api/javafx/scen e/control/RadioButton.html

(89)

Example – RadioButtonExample.java

RadioButtonExample.java

• Example corresponds to the following slides on radio buttons

(90)

RadioButton

@Override

public void start(Stage mainFrame) {

RadioButton radioButton1 = new RadioButton("B1"); RadioButton radioButton2 = new RadioButton("B2"); RadioButton radioButton3 = new RadioButton("B3");

VBox vbox = new VBox();

vbox.getChildren().add(radioButton1); vbox.getChildren().add(radioButton2); vbox.getChildren().add(radioButton3);

scene = new Scene(vbox); mainFrame.setScene(scene); mainFrame.show();

}

(91)

RadioButton

(92)

ToggleGroup

• Need RadioButton’s to function correctly – Only one can be selected at a time

Add all radio buttons to a toggle group – http://

docs.oracle.com/javafx/2/api/javafx/scene/control/ ToggleGroup.html

ToggleGroup toggleGroup = new ToggleGroup(); radioButton1.setToggleGroup(toggleGroup);

radioButton2.setToggleGroup(toggleGroup);

(93)

RadioButton

(94)

ComboBox

Standard drop down box • No choices set by default

• Holds a list of items

Does not have to be test

• http://

docs.oracle.com/javafx/2/api/javafx/scene/cont rol/ComboBox.html

(95)

Example – ComboBoxExample.java

ComboBoxExample.java

• Example corresponds to the following slides on combo boxes

(96)

ComboBox Code

@Override

public void start(Stage mainFrame) {

ComboBox comboBox1 = new ComboBox();

comboBox1.getItems().addAll("Choice 1", "Choice 2", "Choice 3"); comboBox1.setMaxWidth(Double.MAX_VALUE);

comboBox1.setVisibleRowCount(10);

scene = new Scene(comboBox1); mainFrame.setScene(scene);

mainFrame.show(); }

(97)

ComboBox Image

(98)

.setVisibleRowCount(1);

Sets the number of rows that can be seen at a

time without scrolling

(99)

.getItems

.getItems returns the ObservableList of items • comboBox1.getItems().get(0)

Returns the first item of the list of items

• ObservableList – Has .add methodCan add to the list

comboBox2Items.add("Choice " + (i+1));

(100)

.setValue

Sets the value of the ComboBox

The element to be displayed

comboBox1.setValue(comboBox1.getItems().get(0));

(101)

ComboBox Code 2

@Override

public void start(Stage mainFrame) {

ComboBox comboBox2 = new ComboBox();

ObservableList<String> comboBox2Items = comboBox2.getItems(); comboBox2.setMaxWidth(Double.MAX_VALUE);

comboBox2.setVisibleRowCount(10);

for(int i=0; i<20; i++) {

comboBox2Items.add("Choice " + (i+1)); }

comboBox2.setValue(comboBox1.getItems().get(0)); scene = new Scene(comboBox2);

mainFrame.setScene(scene); mainFrame.show();

}

(102)

Another ComboBox Example

(103)

ScrollPane

• The typical scroll bar you see

• Can be both horizontal and vertical – Same scroll bar component does both

• Can add any component to a ScrollPane to make it scrollable

May be too large to display whole componentEx: A paint canvas

• http://

docs.oracle.com/javase/8/javafx/api/javafx/sce

(104)

Example – ScrollPaneExample.java

ScrollPaneExample.java

• Example corresponds to the following slides on scroll panes

(105)

ScrollPane Code

@Override

public void start(Stage mainFrame) {

Image image = new Image("testImage2.png");

ImageView imageView = new ImageView(image);

ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(imageView);

scrollPane.setPrefSize(400, 400);

scene = new Scene(scrollPane); mainFrame.setScene(scene); mainFrame.show();

}

(106)

ScrollPane Images

(107)

ScrollBar Policies

By default the scroll panes will be displayed

when needed

When needed = when the size of the object is

larger than the preferred size of the ScrollPane

Can set if/when to display ScrollPane

ALWAYS = will always display the scrollpaneAS_NEEDED = will display when needed

NEVER = no scroll pane

Can be set for vertical and horizontal separately

(108)

ScrollPane Policy

http://

docs.oracle.com/javase/8/javafx/api/javafx/scene/contr ol/ScrollPane.ScrollBarPolicy.html

scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);

• scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);

• scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);

https://docs.oracle.com/javase/8/javafx/get-started-tut http://docs.oracle.com/javase/8/javafx/api/javafx/stage/St http://docs.oracle.com/javafx/2/api/javafx/scene/package http://docs.oracle.com/javafx/2/api/javafx/scene/Node.ht http://docs.oracle.com/javase/8/javafx/api/javafx/sce http://docs.oracle.com/javafx/2/api/javafx/scene/pain http://docs.oracle.com/javafx/2/api/javafx/scene/pain http://docs.oracle.com/javafx/2/api/javafx/scene/text/Fon http://docs.oracle.com/javafx/2/api/javafx/scene/text/Fon http://docs.oracle.com/javafx/2/api/javafx/scene/text/Fon http://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Borde http://docs.oracle.com/javase/8/javafx/api/javafx/sce http://docs.oracle.com/javafx/2/api/javafx/geometry/ http://docs.oracle.com/javafx/2/api/javafx/scene/image/I http://docs.oracle.com/javafx/2/api/javafx/scene/image/I http://docs.oracle.com/javase/8/javafx/api/javafx/sce http://docs.oracle.com/javase/8/javafx/api/javafx/sce http://docs.oracle.com/javase/8/javafx/api/javafx/scen http://docs.oracle.com/javase/8/javafx/api/javafx/scen http://docs.oracle.com/javafx/2/api/javafx/scene/control/ http://docs.oracle.com/javafx/2/api/javafx/scene/cont http://docs.oracle.com/javase/8/javafx/api/javafx/sce http://docs.oracle.com/javase/8/javafx/api/javafx/scene/contr

References

Related documents

As can be seen from Section 2 the composite emitter - toroid and dipole - becomes a non-radiating configuration (note that the compensation condition (9) is satisfied) due to

(iii) Write the simplified sum-of-products Boolean algebraic expression for the truth table. X =

When adding fields to a document, there might be times when you want to let your recipient select options on the document and you might not be sure if you should use check boxes

BCLC, Barcelona Clinic Liver Cancer; HCC, hepatocellular carcinoma; LTC, local tumor control; OS, overall survival; PBT, proton beam therapy; PFS, progression free survival..

At day 4 of L1 differentiation, we observed expected induction of master regulatory TFs including Cebpa, Pparg, members of the Klf family, and retinoid-x receptor family (Rxr) as

However, this study has found that the implementation of Employment Equity Plan has been not without challenges, particularly with regard to the recruitment of women employees to

• MERA’s M2M R&amp;D team is following this ETSI initiative and designing an M2M gateway prototype according to the GCSL (Gateway Service Capability Layer) ETSI standards • In

Press and release the ‘Station +’ and ‘- Station’ buttons on the radio to move through the station list.. Press and release the ‘Select’ button on the top of the radio