Java Reflection - ACTIVEevents.com

eyelashesnectarineΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

137 εμφανίσεις

San Francisco


September 23, 2013

Come Together Right Now!


Extending Swing Apps with JavaFX

Paul Anderson

Gail Anderson

Anderson Software Group, Inc.

www.asgteach.com


© 2012 Anderson Software Group

1

© 2013 Anderson Software Group


Training Company


Java, JavaFX Courses



JavaFX Authors


Essential JavaFX


JavaFX with NetBeans



Platform (2014)




LiveLesson Videos


Java Reflection


JavaFX (2014)


2


Family Tree Application (FTA)

`
FTA Design Approach


NetBeans Platform


Modules, TopComponents


JavaFX Properties, Binding, Animation


Swing and
JavaFX


JavaFX in TopComponents


FTA Implementation


Communication Strategies


Wrap Up


© 2013 Anderson Software Group

3


Application to build/explore a Family Tree


Based on the NetBeans Platform


Actions to Create, Read, Update, Delete (CRUD)
Persons, Events


Display Timelines


Features


Uses JPA and Java DB for persistence


Database access performed in background


Swing / JavaFX integration


JavaFX for enhanced user experience



© 2013 Anderson Software Group

4

© 2013 Anderson Software Group

5

© 2013 Anderson Software Group

6


Family Tree Application (FTA)


FTA Design Approach


NetBeans Platform


Modules, TopComponents


JavaFX Properties, Binding, Animation


Swing and JavaFX


JavaFX in TopComponents


FTA Implementation


Communication Strategies


Wrap Up


© 2013 Anderson Software Group

7


What Is It
?


R
ich
client
modular framework


Swing
-
based, works with JavaFX


Window manager


Features


Tool bars, Menu bars, Menus, Actions


Selection, Context Menus, Context Actions


Customizable


Saves settings between starts



© 2013 Anderson Software Group

8


Why Use It?


Extensive
set of APIs


Plugins
, modular and
extendable


Service infrastructure


Lifecycle
management


Loosely coupled architecture


Reduces development time


Platform independent


Active community



© 2013 Anderson Software Group

9


Modules


Collection of related classes


Public
interfaces, library wrappers


Module dependencies


Loosely coupled


Platform Distribution


Modules, Window System, Lookup, File System


Nodes, Explorer & Property Sheet, Actions


Dialogs, UI Utilities, Visual Library, I/O,
Progress


© 2013 Anderson Software Group

10


Runtime Container


Loads modules dynamically as needed


Unloads, installs, uninstalls modules at run time


Bootstrap, Startup


Module and File System APIs, Lookup & Utilities APIs


Classloader System


Part of the NetBeans runtime container


Supports the encapsulation of modules


Enables class loading and namespace integrity

© 2013 Anderson Software Group

11


Module Dependencies


Contract between two modules


Module A declares public packages


Module B sets dependency on Module A


Enforces loose
coupling


© 2013 Anderson Software Group

12


Lookup


Communication between modules


Global


application
-
wide classes and data


Instance Content


container for Lookups


Why Use Lookup?


Provides loose coupling between modules


Provides context sensitivity for Windows and Data


Can be queried and listened to


Map of service providers



© 2013 Anderson Software Group

13


What are Top Components?


Basic unit of display


Extend
JComponent


Integrated into window system


Singleton or non
-
singleton


Dock/undock/minimize/maximize/slide


Open at startup or not


Persist properties between starts


Configure with Java annotations

© 2013 Anderson Software Group

14


Steps to Create a NetBeans RCP Application


1.
File
-
> New
-
> NetBeans Platform Application project

2.
**Modules
-
> New wrapped library JavaFX Runtime

3.
Modules
-
> Add new module

4.
**New Module
-
> Libraries
-
> Add module dependency

5.
New Module
-
> New Window (TopComponent)

6.
Add JavaFX content to TopComponent


**Not necessary in Java 8



© 2012 Anderson Software Group

15


Family Tree Application (FTA
)


FTA Design
Approach


NetBeans Platform


Modules, TopComponents


JavaFX Properties, Binding, Animation

?C
Swing and
JavaFX


JavaFX in TopComponents


FTA Implementation


Communication Strategies


Wrap Up


© 2013 Anderson Software Group

16


What are Properties?


Similar to JavaBean component properties


Observable


Why Use Properties?


Encapsulates data with event delivery


All JavaFX controls provide properties


Use with binding
expressions


© 2013 Anderson Software Group

17


What Is Binding?


Lets properties participate in dependencies with
other property values


Allows UI controls to synchronize with their model
data


Why Use Binding?


Avoids writing event handlers and listeners


More concise


Less error prone

© 2013 Anderson Software Group

18


Simple Bind


Update property when dependent property changes


Bidirectional


P
roperty updates in both directions


Fluent API


Binds properties with binding expression APIs


Custom Binding


Specify property dependencies and compute values

© 2013 Anderson Software Group

19


Unidirectional


Dependency
in one direction


Between property object and observable value


Cannot explicitly update bound properties


Example


label.textProperty()


.bind(textField.textProperty());

© 2013 Anderson Software Group

20


Bidirectional


Dependencies
in both directions


Between two property objects


May update bound properties


Example


person.firstNameProperty
()


.bindBidirectional(
textField.textProperty());

© 2013 Anderson Software Group

21


Built
-
in Library Expressions


Booleans and Numbers


Strings and Objects


Example


DoubleProperty a = new SimpleDoubleProperty(3.5);


DoubleProperty b = new SimpleDoubleProperty(4.5);


BooleanBinding show = a.isEqualTo(b);


circle.visibleProperty().bind(show)
;

© 2013 Anderson Software Group

22

© 2013 Anderson Software Group

23


What is Custom Binding?


When you can’t use the Fluent API


Lower level


More flexible, better performance


Format


XXX
Binding
myBinding

= new
XXX
Binding() {


{ super.bind
(
dependentProperties
)
; }


@Override


protected
bindingType

computeValue
() { . . . }


};


© 2013 Anderson Software Group

24

© 2013 Anderson Software Group

25


Transitions


High level animations


Change one or more node properties over time


Working with Transitions


Specify node and duration


Interpolator Options


LINEAR, EASE_IN, EASE_OUT, EASE_BOTH


FTA Transitions


FadeTransition, ScaleTransition, SequentialTransition

© 2012 Anderson Software Group

26


Fade Transition


Changes opacity property

© 2012 Anderson Software Group


Example

FadeTransition ft = new FadeTransition(



Duration.millis(1500), numberLabel);

ft.setInterpolator(Interpolator.EASE_OUT);

ft.setToValue(0);

ft.playFromStart();

27


ScaleTransition


Changes scaleX, scaleY, scaleZ properties

© 2012 Anderson Software Group


Example

ScaleTransition st = new ScaleTransition(



Duration.millis(1000), imageViewNode);

st.setToX(2.0);

st.setToY(2.0);

st.play();

28


SequentialTransition


Groups multiple transitions


Executes sequentially


© 2012 Anderson Software Group


Example

SequentialTransition seqtran =



new SequentialTransition(st, ft);

seqtran.playFromStart();

29


Parallel Transition


Groups multiple transitions


Executes in parallel


© 2012 Anderson Software Group


Example

ParallelTransition pt =



new ParallelTransition(st, ft);

pt.playFromStart();

30


Family Tree Application (FTA
)


FTA Design
Approach


NetBeans Platform


Modules, TopComponents


JavaFX Properties, Binding, Animation


Swing and
JavaFX


JavaFX in TopComponents


FTA Implementation


Communication Strategies


Wrap Up


© 2013 Anderson Software Group

31


The Magic of
JFXPanel


Package
javafx.embed.swing


Embed JavaFX content into Swing
apps


Creating
JFXPanel

instance starts JavaFX runtime


Input/focus events forwarded to scene


Example



JFrame
frame = new JFrame(
"MyApp");



JFXPanel
fxPanel = new JFXPanel();



frame.add
(fxPanel);



© 2013 Anderson Software Group

32


Swing Components


Create and update
on Event Dispatch Thread (EDT
)


Single
-
threaded UI


JavaFX Components


Create and update on JavaFX Application Thread


Single
-
threaded UI


Separate
t
hread from EDT


© 2013 Anderson Software Group

33


Thread Scenarios


JavaFX event updates Swing component


Swing event updates JavaFX scene


Synchronize with JavaFX thread


JavaFX 8 Update


JavaFX/Swing threads to merge


Experimental API


Must enable explicitly


Simplifies JavaFX/Swing coding

© 2013 Anderson Software Group

34


Swing Thread (EDT)


Wrap Code in
Runnable

Object


SwingUtilities.invokeLater(new Runnable() {



@Override



public void run() {



// change Swing data



}



)};

© 2013 Anderson Software Group

35


JavaFX Application Thread


Wrap Code in
Runnable

Object


Platform.runLater(new Runnable() {



@Override



public void run() {




// change JavaFX data



}



)};

© 2013 Anderson Software Group

36


Synchronize with JavaFX Thread


final CountDownLatch latch = new CountDownLatch(1);


Platform.runLater(new Runnable() {


@Override public void run() {



try { controller.updateForm(person); }


finally { latch.countdown();}


}


});


try {


latch.await(); // wait to finish


this.readyToListen = true;


} catch (InterruptedException ex) { . . . }

© 2012 Anderson Software Group

37


Swing/JavaFX Single
-
Threaded UIs


Background Threads Keep UI Responsive


SwingWorker / JavaFX Tasks and Services


Execute code in background thread


Provide handshaking between background thread
and UI thread


Callback methods safely update UI


© 2013 Anderson Software Group

38


Family Tree Application (FTA
)


FTA Design
Approach


NetBeans Platform


Modules, TopComponents


JavaFX Properties, Binding, Animation


Swing and
JavaFX


JavaFX in TopComponents


FTA Implementation


Communication Strategies


Wrap Up


© 2013 Anderson Software Group

39


Design Approach


Use FXML


Helps isolate JavaFX/Swing code


Optionally use Scene Builder for UI creation


Creates 3 Files


TopComponent


FXML


JavaFX Controller

© 2013 Anderson Software Group

40


TopComponent Role


Swing UI JComponent


Integrates into NB Platform Window System


JFXPanel holds scene graph


Create JavaFX scene graph with FXMLLoader


Get handle to Controller


Communicate with NB Platform state


Respond to changes in JavaFX scene graph

© 2013 Anderson Software Group

41


JavaFX FXML


JavaFX static content


Use Scene Builder or edit manually


JavaFX Controller Class


Initialization code for FXML
-
defined controls


Binding, Animation


Event handlers for JavaFX controls


Public methods that update scene graph, can be
invoked from TopComponent

© 2013 Anderson Software Group

42


FXML


<Text fx:id="text" fill="DARKSLATEGRAY"


text="Hello from JavaFX !" />


JavaFX Controller


@
FXML


private
Text text
;



String what = text.getText();

© 2013 Anderson Software Group

43


What is Scene Builder?


Drag and drop tool


Generates FXML


Binds UI to application logic

Why Use Scene Builder?


Easy to use


Lets you design
GUIs without
writing code


Fast prototyping


Helps visualize scene graph structure

© 2013 Anderson Software Group

44

© 2013 Anderson Software Group

45


Example


fxPanel = new JFXPanel()
;


add(fxPanel, BorderLayout.CENTER);


Platform.setImplicitExit(false)
;



Platform.runLater(new Runnable()
{



@Override public void run()
{



createScene()
;



}



}

© 2013 Anderson Software Group

46


Create Scene With FXML, Controller Access



private void createScene() {


try {


URL location = getClass().getResource
("
Welcome.fxml");


FXMLLoader fxmlLoader = new FXMLLoader();


fxmlLoader.setLocation(location);


Parent root = (Parent)fxmlLoader.load(



location.openStream());


fxPanel.setScene(new Scene(root));


controller = (WelcomeController)



fxmlLoader.getController();


} catch (IOException ex) { . . . }


}

© 2013 Anderson Software Group

47


Family Tree Application (FTA
)


FTA Design Approach


NetBeans Platform


Modules, TopComponents


JavaFX Properties, Binding, Animation


Swing and
JavaFX


JavaFX in TopComponents


FTA Implementation


Communication Strategies


Wrap Up


© 2013 Anderson Software Group

48

© 2013 Anderson Software Group

49


JavaFX Thread
-
Contained Events


No
communication between JavaFX, TopComponent


Static JavaFX Content


L
ocal
to JavaFX scene


JavaFX content responds to user input


Animations


Scene graph changes


Example


Welcome Window

© 2013 Anderson Software Group

50

© 2013 Anderson Software Group

51


Update JavaFX Content From TopComponent


One
-
way communication


TopComponent listeners respond to property change
events or lookup events


TopComponent invokes JavaFX methods to update
scene graph


Example


TimeLine Window

© 2013 Anderson Software Group

52

© 2013 Anderson Software Group

53


Update JavaFX Content and TopComponent


Two
-
way communication


JavaFX fires property change events to update
TopComponent


TopComponent invokes JavaFX controller methods to
update JavaFX content


Use
CountDownLatch

for handshaking


W
ait
for scene graph
updates


Example


Person Editor, Event Editor

© 2013 Anderson Software Group

54

© 2013 Anderson Software Group

55

© 2013 Anderson Software Group

56


Features Update


JavaFX on the classpath


Builders are deprecated


JavaFX / Swing Thread can be merged


Swing Node


counterpart to JFXPanel


Improved 3D Support


Improved/additional controls

© 2013 Anderson Software Group

57


NetBeans Platform


Stable, content
-
rich, modular framework


Window system, service provider, action framework


JavaFX


Rich GUI, binding, animation


Can integrate into NB platform window


Communication


Use FXML, Controller class for MVC design


Use strategies to keep JavaFX / Swing isolated


© 2013 Anderson Software Group

58


Thanks for Coming!


paul@asgteach.com

@paul_asgteach


gail@asgteach.com

@gail_asgteach


Source Code


FamilyTreeApp


http://www.asgteach.com/download/
FamilyTre
eApp.zip


README
File to
configure database


Q & A


© 2013 Anderson Software Group

59