JavaFX Components
JavaFX
• Graphics packages
• Combines old awt and swing libraries
• Can run in web browsers
Basics
• Class must extend Application class
• Class must override and implement the start method
• All components attached to the main stage
Example – SceneExample.java
• Example – SceneExample.java
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 you – Should always have main method anyway
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
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
Scene Graph
• Hierarchical tree of nodes for all visual
elements
• Applying transformations to a parent node can also apply to the child nodes
• https://
Node
• A node is any component or layout
• Nodes are attached to other nodes
• Nodes are attached to scenes
• Examples
– Buttons
– Checkboxes – Panels
• Which can contain more nodes
setScene
• Method of stage class
• Sets the scene to be displayed
• Can use this to swap scenes when using multiple scenes (cool!)
show
• Method of stage class • Displays the scene
• Don’t forget to call this once scene is setup
sizeToScene
• Method of Window class
• Sizes the stage (window) to the size of the scene it contains
• Basically an auto-size feature
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");
setTitle
Summary so far
• Stage = window
– http://
docs.oracle.com/javase/8/javafx/api/javafx/stage/St age.html
• Stage contains one scene at a time • Scene = 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
Components
• Button • TextField • TextArea • Label • RadioButton • CheckBox • ComboBox• Image + ImageView • ScrollPane
Methods
Several Components Use The Same Methods
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
Example – ButtonExample.java
• ButtonExample.java
• Example corresponds to the following slides on buttons
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
setText: Before Setting Text
setText: After Setting Text
setText: After sizeToScene
setTextFill
• Sets the paint of the button text • Typically a color is used
– Color is a sub class of Paint
Paint class
• Abstract class
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
setTextFill
• button2.setTextFill(Color.BLUE);
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
.setPrefWidth
• Sets the width of the component
• This width will usually be displayed
• May need to call sizeToScene
.setPrefWidth
• button3.setPrefWidth(200);
.setPrefHeight
• Sets the height of the component
• This height will usually be displayed
• May need to call sizeToScene
.setPrefHeight
• button3.setPrefHeight(200);
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
.setMaxWidth
• No max width set for button 1
.setMaxWidth
• button1.setMaxWidth(Double.MAX_VALUE)
;
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
.setMaxHeight
• No max height set for button 6
.setMaxHeight
• button6.setMaxHeight(Double.MAX_VALUE);
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 component • setPrefHeight = typical height of component
• setMinWidth = Component will never have width smaller than min width
• setMinHeight = Component will never have height smaller than min height
.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
.setFont
• button4.setFont(Font.font("Times New Roman", 20));
.setFont
button4.setFont(Font.font
("Times New Roman", FontWeight.BOLD, 20));
.setFont
button4.setFont(Font.font
("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 20));
.setBorder
• Sets the border of the component • Need to create a Border
• Need to create a BorderStroke first
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
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
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();
Border Example 1
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();
Border Example 2
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();
Border Example 3
Alignment
• Can set position of the component if it is not set
to fill it’s entire space
– Horizontal position
• Center
• Left • Right
– Vertical position
• Top
• Center
• Bottom
Alignment
• Set preferred height for button3 to 200 • Set preferred width for button5 to 200
Alignment
• Default alignment for button6
Alignment
• button6.setAlignment(Pos.CENTER);
Alignment
• button6.setAlignment(Pos.TOP_LEFT);
Alignment
• button6.setAlignment(Pos.BOTTOM_RIGHT)
;
Alignment
• Pos enum in javafx.geometry class • http://
docs.oracle.com/javafx/2/api/javafx/geometry/ Pos.html
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
Image + ImageView Class
• Load an image from a file
• Image
– Directly stores the image
• ImageView
– Can manipulate the image
• Rotate
• Scale • Etc…
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
Button Image
Image image = new Image("testImage.png"); ImageView imageView = new
ImageView(image);
button6.setPrefHeight(200); button6.setPrefWidth(200);
button6.setGraphic(imageView); mainFrame.sizeToScene();
Button Image
Button Image
button6.setContentDisplay(ContentDisplay.TOP);
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
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
Example – TextFieldExample.java
• TextFieldExample.java
• Example corresponds to the following slides on text fields
TextField Image
setFont
• textField.setFont(Font.font("Arial Black", 20));
– TextField got bigger to fit the text
.setAlignment
• textField.setAlignment(Pos.CENTER);
.setText and .getText
• textField.setText("LOWER CASE");
• textField.setText(textField.getText().toLowerCase());
.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);
.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);
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
Example – TextAreaExample.java
• Example – TextAreaExample.java
• Example corresponds to the following slides on text areas
TextArea
@Override
public void start(Stage mainFrame) {
TextArea textArea = new TextArea(); scene = new Scene(textArea);
mainFrame.setScene(scene); mainFrame.show();
}
TextArea
.setWrapText
• Sets whether the text will wrap or not • textArea.setWrapText(false);
• textArea.setWrapText(true);
.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);
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");
Example – LabelExample.java
• Example – LabelExample.java
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");
Example – CheckBoxExample.java
• CheckBoxExample.java
.setSelected
• checkBox.setSelected(true);
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
RadioButton
• Standard radio button you typically see • Usually in groups of two or more
– Usually mutually exclusive
• Only one can be selected at a time
– By default they are not mutually exclusive – Need 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
Example – RadioButtonExample.java
• RadioButtonExample.java
• Example corresponds to the following slides on radio buttons
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();
}
RadioButton
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);
RadioButton
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
Example – ComboBoxExample.java
• ComboBoxExample.java
• Example corresponds to the following slides on combo boxes
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(); }
ComboBox Image
.setVisibleRowCount(1);
• Sets the number of rows that can be seen at a
time without scrolling
.getItems
• .getItems returns the ObservableList of items • comboBox1.getItems().get(0)
– Returns the first item of the list of items
• ObservableList – Has .add method – Can add to the list
comboBox2Items.add("Choice " + (i+1));
.setValue
• Sets the value of the ComboBox
– The element to be displayed
• comboBox1.setValue(comboBox1.getItems().get(0));
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();
}
Another ComboBox Example
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 component – Ex: A paint canvas
• http://
docs.oracle.com/javase/8/javafx/api/javafx/sce
Example – ScrollPaneExample.java
• ScrollPaneExample.java
• Example corresponds to the following slides on scroll panes
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();
}
ScrollPane Images
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 scrollpane – AS_NEEDED = will display when needed
– NEVER = no scroll pane
• Can be set for vertical and horizontal separately
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);