Gui Slide en
Gui Slide en
checkbox.addItemListener( this::onItemChanged );
...
}
}
Swing
Extends the core concepts and mechanisms of AWT;
we still have components, containers, layout
managers, events and event listeners.
Replaces completely the AWT componet set,
providing a new set of components, capable of sorting,
printing, drag and drop and other “cool” features.
Brings portability to the GUI level; no more native
peers, all components are “pure”.
Based on Separable Model-and-View design pattern.
"Component Oriented Programming"
Swing Components
Atomic Components
JLabel, JButton, JCheckBox, JRadioButton, JToggleButton,
JScrollBar, JSlider, JProgressBar, JSeparator
Complex Components
JTable, JTree, JComboBox, JSpinner, JList, JFileChooser,
JColorChooser, JOptionPane
Text Editing Components
JTextField, JFormattedTextField, JPasswordField, JTextArea,
JEditorPane, JTextPane
Menus
JMenuBar, JMenu, JPopupMenu, JMenuItem,
JCheckboxMenuItem, JRadioButtonMenuItem
Intermediate Containers
JPanel, JScrollPane, JSplitPane, JTabbedPane, JDesktopPane,
JToolBar
High-Level Containers
JFrame, JDialog, JWindow, JInternalFrame, JApplet
Similarities and Differences with
AWT
"J" Convention
java.awt.Frame – javax.swing.JFrame
java.awt.Button - javax.swing.JButton
java.awt.Label - javax.swing.JLabel
...
Working with Texts
●
Font - A collection of glyphs (unique marks that collectively
add up to the spelling of a word) → name, style, size
Label label = new Label("Some text");
label.setFont(new Font("Dialog", Font.PLAIN, 12));
void paint(Graphics g) {
g.setFont(new Font("Courier", Font.BOLD, 10));
g.drawString("Another text", 10, 20); }
●
FontMetrics - encapsulates information about the
rendering of a particular font on a particular screen.
Font f = new Font("Arial", Font.BOLD, 11);
FontMetrics fm = g.getFontMetrics();
int height = fm.getHeight();
int width = fm.stringWidth("frog");
int xWidth = fm.charWidth(’g’);
●
TextLayout - highlighting, strings with mixed fonts, mixed languages, bidirectional text.
Using Colors
●
Paint interface defines how color patterns can be
generated for Graphics2D operations.
●
Color encapsulates colors in the sRGB space
Red Green Blue Alpha
Color standardRed = Color.RED;
(0 − 255, 0.0 − 1.0)
Color plainWhite = new Color(1.0, 1.0, 1.0);
Color translucentRed = new Color(255, 0, 0, 128);
●
SystemColor encapsulate symbolic colors representing
the color of native GUI objects on a system.
SystemColor.desktop
●
GradientColor provides a way to fill a Shape with a linear
color gradient pattern. Hello world!
●
TexturePaint provides a way to fill a Shape with a texture
that is specified as a BufferedImage. Hello again...
Using Images
●
Image is the superclass of all classes that
represent graphical images.
●
BufferedImage
– Loadind from a file
BufferedImage image = ImageIO.read(new File("hello.jpg"))
javafx.scene.Parent
javafx.scene.Parent
The
Thebase
baseclass
classforforall
allnodes
nodesthat
that
have
havechildren
childrenininthe
thescene
scenegraph
graph
javafx.scene.Region
The base class for all JavaFX
Node-based UI Controls, and all
layout containers.
javafx.scene.Pane
Base class for layout panes
javafx.scene.Control
Base class for all user interface
Each item in the scene graph is called a Node. controls.
Each node in the scene graph can be given a unique id.
Each node has a bounding rectangle and a style.
Any Node can have transformations applied to it: translation, rotation, scaling, or shearing.
Layout Management
Setting the position and size for UI element.
●
A “combo” of a Swing JPanel + LayoutManager
●
javafx.scene.layout.Pane - Base class for layout
panes; used directly in cases where absolute
positioning of children is required.
●
Uses preffered, minimum and maximum properties
●
FlowPane, BorderPane,
AnchorPane, StackPane,
TilePane, GridPane,
TextFlow, HBox, VBox, etc.
● borderPane.setCenter(
new ListView());
borderPane.setBottom(
new Label("Hello"));
Adding Functionality
public class HelloWorld extends Application {
@Override
public void start(Stage primaryStage) {
Button helloBtn = new Button();
helloBtn.setText("Hello World!");
helloBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello Button was clicked!");
}
});
ScaleTransition scale =
new ScaleTransition(Duration.millis(750));
scale.setToX(0.1); scale.setToY(0.1);
ParallelTransition transition =
new ParallelTransition(blueSquare,
translate, fill, rotate, scale);
transition.setCycleCount(Timeline.INDEFINITE);
transition.setAutoReverse(true);
transition.play();
Pulse
●
A pulse is an event that indicates to the JavaFX scene graph
that it is time to synchronize the state of the elements on the
scene graph with Prism.
●
A pulse is throttled at 60 frames per seconds (fps) maximum
and is fired whenever animations are running or when
something in the scene graph is changed. For example, if a
position of a button is changed, a pulse is scheduled.
●
When a pulse is fired, the state of the elements on the scene
graph is synchronized down to the rendering layer.
●
A pulse enables application developers a way to handle events
asynchronously. This important feature allows the system to
batch and execute events on the pulse.
●
The Glass Windowing Toolkit is responsible for executing the
pulse events. It uses the high-resolution native timers to make
the execution.
Styling withs CSS
Cascading Style Sheets
●
Define Style Sheets Files
.root {
-fx-background-image: url("background.jpg");
}
.label {
-fx-font-size: 12px;
-fx-font-weight: bold;
-fx-text-fill: #333333;
}
●
Specify the CSS
scene.getStylesheets().add("path/stylesheet.css");
●
Inline
helloBtn.setStyle(
"-fx-background-color: slateblue; " +
"-fx-text-fill: white;");
FXML
●
XML-based language that provides the structure for
building a user interface separate from the
application logic of your code.
●
Java (Programatic)
BorderPane border = new BorderPane();
Label helloLabel = new Label("Hello");
border.setTop(helloLabel);
Label worldLabel = new Label ("World");
border.setCenter(worldLabel);
●
FXML (Declarative)
<BorderPane>
<top>
<Label text="Hello"/> JavaFX Scene Builder
</top>
<center>
<Label text="World"/>
</center>
</BorderPane>
Using FXML to Create UI
●
FXML Loader
Parent root = FXMLLoader.load(
getClass().getResource("example.fxml"));
Scene scene = new Scene(root, 300, 275);
●
Create the link between view and control
<GridPane fx:controller="FXMLExampleController">
<Button text="Sign In"
onAction="#handleSubmitButtonAction"/>
<Text fx:id="actiontarget" />
</GridPane>
●
Define the code to handle events
public class FXMLExampleController {
@FXML
private Text actiontarget;
@FXML
protected void handleSubmitButtonAction(ActionEvent event) {
actiontarget.setText("Sign in button pressed");
}
}
Swing or JavaFX?
●
Swing
➢ Maturity,
Stability
➢ Component Libraries and Frameworks
➢ Large amount of resources
●
JavaFX
➢ Modern, MVC friendly, CSS, FXML
➢ Spectacular (3D, Animations, etc.)
➢ May not be “rock-solid” in production, yet
➢ Not so many resources