GUI
System integration for desktop applications
Java, winter semester 2016 11.1.2017
1
java.awt.Desktop ● ●
system integration for desktop applications static boolean isDesktopSupported() –
●
static Desktop getDesktop() –
●
opens the default mail client
void open(File file) –
●
opens the file in the default editor for the given file type
void mail(URI mailtoURI) –
●
opens an uri in the default browser
void edit(File file) –
●
returns an instance of the desktop
void browse(URI uri) –
●
whether the desktop integration is supported
opens the file in the default program for the given file type
void print(File file)
Java, winter semester 2016 11.1.2017 –
prints file
2
java.awt.Desktop ●
boolean isSupported(Desktop.Action action) – –
what is supported Desktop.Action ● ●
enum values – – – – –
BROWSE EDIT MAIL OPEN PRINT
Java, winter semester 2016 11.1.2017
3
java.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 2016 11.1.2017
4
java.awt.SystemTray ●
right-click on the icon –
●
left-click –
● ●
shows menu
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 2016 11.1.2017
5
JAVA
Applet
Java, winter semester 2016 11.1.2017
6
–
in fact no access to the computer ● ● ●
● ●
CA TE
●
applet = a "small" program running in a browser security limitations 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)
D EP
– –
RE
●
D
Overview
●
start() –
●
stop() –
●
called always when the applet is shown called always when the applet is hidden
destroy()
Java, winter semester –2016 called 11.1.2017
during termination
7
JAVA
JNLP Java Web Start
Java, winter semester 2016 11.1.2017
8
Overview ● ● ●
Java Network Launch Protocol (JNLP) advantages of both regular applications and applets an application is downloaded over the network –
● ●
it is saved to the disk and launched in a sandbox in later launches, first a version is checked – –
●
a regular application
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 2016 11.1.2017
9
Creating 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 Example Vendor Description Java, winter semester 2016 10 11.1.2017
Java
JavaFX
Java, summer semester 2015 18.2.2015
Overview ●
Java GUI –
Java 1.0 (1996) – AWT ●
–
Java 1.2 (2000) – Swing ●
–
using native GUI components GUI completely in Java
JavaFX (2007) ● ● ●
new technology running on the Java VM but own language –
● ●
–
intended as a competitor to Flash failed
JavaFX 2.0 (2011) ●
– –
declarative
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 2015 18.2.2015
Hello World public 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 J prefix
// no main() }
Java, summer semester 2015 18.2.2015
Hello World ●
explicit main() necessary in older version public class MyApp extends Application { public static void main(String[] args) { launch(args); } ... }
Java, summer semester 2015 18.2.2015
Events ● ●
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 2015 18.2.2015
Properties of components ●
●
interface Property – –
void addListener(InvalidationListener listener) void addListener(ChangeListener