system integration for desktop applicationshnetynka/teaching/java/slides2016/java13.en.pdf · java,...
TRANSCRIPT
Java, winter semester 201611.1.2017 1
GUIGUI
System integration fordesktop applications
Java, winter semester 201611.1.2017 2
java.awt.Desktopjava.awt.Desktop● system integration for desktop applications● static boolean isDesktopSupported()
– whether the desktop integration is supported● static Desktop getDesktop()
– returns an instance of the desktop● void browse(URI uri)
– opens an uri in the default browser● void edit(File file)
– opens the file in the default editor for the given file type● void mail(URI mailtoURI)
– opens the default mail client● void open(File file)
– opens the file in the default program for the given file type
● void print(File file)– prints file
Java, winter semester 201611.1.2017 3
java.awt.Desktopjava.awt.Desktop● boolean isSupported(Desktop.Action action)
– what is supported– Desktop.Action
● enum● values
– BROWSE– EDIT– MAIL– OPEN– PRINT
Java, winter semester 201611.1.2017 4
java.awt.SystemTrayjava.awt.SystemTray● represents the system “tray”● example
TrayIcon trayIcon = null;if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); Image image = ... ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { ... } }; PopupMenu popup = new PopupMenu(); popup.add(...); trayIcon = new TrayIcon(image, "Tray Demo", popup); trayIcon.addActionListener(listener); tray.add(trayIcon);}
Java, winter semester 201611.1.2017 5
java.awt.SystemTrayjava.awt.SystemTray● right-click on the icon
– shows menu● left-click
– generates the action event● a single application can add any number of icons● methods
– static boolean isSupported()– void add(TrayIcon icon)– void remove(TrayIcon icon)
● removes the icon from the tray– when the application terminates the icons are moved
automatically– TrayIcon[] getTrayIcons()
● returns all tray icons of the application
Java, winter semester 201611.1.2017 6
JAVAJAVA
Applet
Java, winter semester 201611.1.2017 7
OverviewOverview● applet = a "small" program running in a browser● security limitations
– in fact no access to the computer● limited access to a disk● limited access to a net● ...
● "signing" applets!!!● implementation – extending javax.swing.JApplet
– methods● init()
– called during initialization– WARNING – it is necessary to use
SwingUtilities.invokeAndWait(Runnable r)● start()
– called always when the applet is shown ● stop()
– called always when the applet is hidden● destroy()
– called during termination
DEPRECATED
DEPRECATED
Java, winter semester 201611.1.2017 8
JAVAJAVA
JNLPJava Web Start
Java, winter semester 201611.1.2017 9
OverviewOverview● Java Network Launch Protocol (JNLP)● advantages of both regular applications and applets● an application is downloaded over the network
– a regular application● it is saved to the disk and launched in a sandbox● in later launches, first a version is checked
– if it is the same, the application is run from the disk– if it changed, the application is downloaded
● similar security limitations like for applets– applications can be signed– even non-signed application can have access to the
computer● via JNLP API
● JNLP is only a specification– implementation – Java Web Start
Java, winter semester 201611.1.2017 10
Creating a JNLP applicationCreating a JNLP application● a regular application packed in a JAR file● plus a descriptor file
– xml, the jnlp filename extension● Web server – mime.conf
– application/x-java-jnlp-file JNLP<?xml version="1.0" encoding="UTF-8"?> <jnlp codebase="http://somewhere/" href="hello.jnlp"> <information> <title>Example</title> <vendor>Vendor</vendor> <description>Description</description> <icon href="icon.png"/> <offline-allowed/> </information> <resources> <j2se version="1.4+"/> <jar href="hello.jar"/> </resources> <application-desc main-class="MainClass" /></jnlp>
Java, summer semester 201518.2.2015
JavaJava
JavaFX
Java, summer semester 201518.2.2015
OverviewOverview● Java GUI
– Java 1.0 (1996) – AWT● using native GUI components
– Java 1.2 (2000) – Swing● GUI completely in Java
– JavaFX (2007)● new technology● running on the Java VM● but own language
– declarative● intended as a competitor to Flash● failed
– JavaFX 2.0 (2011)● only API (own language abandoned)
– since JDK 7 update 6 a part of std JDK (JavaFX 2.2)– JDK 8 – JavaFX 8
● no further development of Swing
Java, summer semester 201518.2.2015
Hello WorldHello Worldpublic class HelloWorld extends Application {
@Override public void start(Stage stage) { Label message = new Label("Hello, JavaFX!"); message.setFont(new Font(100)); stage.setScene(new Scene(message)); stage.setTitle("Hello"); stage.show(); } // no main()}
no J prefix
Java, summer semester 201518.2.2015
Hello WorldHello World● explicit main() necessary in older version
public class MyApp extends Application { public static void main(String[] args) { launch(args); } ...}
Java, summer semester 201518.2.2015
EventsEvents● similar as in Swing/AWT● BUT typically – listening on property changes
– i.e. not listening directly on components● there are exceptions, e.g. buttons
Slider slider = new Slider(10, 100, 100);Label message = new Label("Hello, JavaFX!"); slider.valueProperty().addListener(property -> message.setFont(new Font(slider.getValue())));
Java, summer semester 201518.2.2015
Properties of componentsProperties of components● interface Property<T>
– void addListener(InvalidationListener listener)– void addListener(ChangeListener<? super T> listener)
– void bind(ObservableValue<? extends T> observable)– void bindBidirectional(Property<T> other)– …
● implementace– class ObjectProperty<T>– class IntegerProperty– class BooleanProperty– class StringProperty– ...
Java, summer semester 201518.2.2015
Properties – implementation ex.Properties – implementation ex.private StringProperty text =
new SimpleStringProperty("");
public final StringProperty textProperty() { return text; }
public final void setText(String newValue){ text.set(newValue);}
public final String getText() { return text.get(); }
Java, summer semester 201518.2.2015
Properties – listenersProperties – listeners● InvalidationListener
– called if the current property value is not valid anymore– allows for “lazy” evaluation
void invalidated(Observable observable)
● ChangeListener– called if the current property value has changed– it is necessary to evaluate the new value– does not allow for “lazy” evaluation
void changed(ObservableValue<? extends T> observable, T oldValue, T newValue)
Java, summer semester 201518.2.2015
Properties – bindingProperties – binding● automated updating of a property when another one
is changed– internally implemented via listeners
text1.textProperty().bind(text2.textProperty());
text1.textProperty().bindBidirectional(text2.textProperty());
● class Bindings– static methods for easy creation of bindings
Java, summer semester 201518.2.2015
LayoutLayout● creating layout of components
– JavaFX Scene Builder● GUI build visually by “clicking”
– “manually”● creating instances of components
– definition in FXML
● similar as in Swing– panes instead of layout managers
Java, summer semester 201518.2.2015
LayoutLayout● BorderPane (as BorderLayout)● HBox● VBox● GridPane (as GridBagLayout)● TilePane (as GridLayout)● …
VBox pane = new VBox();pane.getChildren().addAll(button1, button2,);pane.setPadding(new Insets(10));
Java, summer semester 201518.2.2015
FXMLFXML<?import java.net.*?><?import javafx.geometry.*?><?import javafx.scene.control.*?><?import javafx.scene.layout.*?><?import javafx.scene.text.*?>
<GridPane fx:controller="fxmlexample.FXMLExampleController"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="10" left="25"/></padding>
<Text text="Welcome" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="2"/> <Label text="User Name:" GridPane.columnIndex="0" GridPane.rowIndex="1"/> ...
Java, summer semester 201518.2.2015
FXMLFXML● usage
public void start(Stage stage) { try { Parent root = FXMLLoader.load(getClass().
getResource("file.fxml")); stage.setScene(new Scene(root)); stage.show(); } catch (IOException ex) { ex.printStackTrace(); System.exit(0); }}
Java, summer semester 201518.2.2015
FXMLFXML● adding listeners/bindings
a)<TextField id="username" …
TextField username = (TextField) root.lookup("#username");
b)<GridPane xmlns:fx="http://javafx.com/fxml" hgap="10" vgap="10" fx:controller="LoginDialogController"><Button fx:id="okButton" text="Ok" />
public class LoginDialogController implements Initializable { @FXML private TextField username; @FXML private Button okButton; public void initialize(URL url, ResourceBundle rb){ okButton.disableProperty().bind(...
Java, summer semester 201518.2.2015
CSSCSS● visual appearance of UI can be set via CSS
Scene scene = new Scene(pane);scene.getStylesheets().add("scene.css");
● JavaFX specific attributes are used
#pane { -fx-padding: 0.5em; -fx-hgap: 0.5em; -fx-vgap: 0.5em; -fx-background-image: url("bg.jpg")}
Java, summer semester 201518.2.2015
Animace apod.Animace apod.● ScaleTransition
ScaleTransition st = new ScaleTransition(Duration.millis(3000));
st.setByX(1.5);st.setByY(1.5);st.setNode(button);st.setCycleCount(Animation.INDEFINITE);st.setAutoReverse(true);st.play();
● FadeTransition● RotateTransition● DropShadow● ...
Java, summer semester 201518.2.2015
Misc componentsMisc components● JavaFX contains
– WebKit (HTML rendering engine)– media player– ...
Java, summer semester 201518.2.2015
JavaJava
What next...
Java, summer semester 201518.2.2015
What nextWhat next● NPRG021 Advanced programming for Java platform
– summer 2/1– synopsis
● Reflection API● Generics, annotations● ClassLoaders, Security ● RMI● JavaBeans● Java Enterprise Edition: EJB, Servlets, Java Server Pages,
Spring,...● Java Micro Edition: Java for mobile and embedded systems,
CLDC, MIDP, MEEP● RTSJ, Java APIs for XML, JDBC, JMX,...● Android
– partially mandatory for NPRG059 Advanced Programming Praxis● a mandatory course for several Master study branches
● NPRG044 Platformy NetBeans and Eclipse
Java, winter semester 201611.1.2017 30This slides are licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Slides version J13.en.2016.01