Demo.java
Download Demo.java
1: // WebStart Demo - A simple demo of using Java WebStart
2: // Written 1/2012 by Wayne Pollock, Tampa Florida USA
3: //
4: // This app should be bundled in WebStartDemo.jar, and is launched
5: // by putting a link to the associated Demo.jnlp file.
6: // Compile with the file javaws.jar listed on CLASSPATH, or it won't work.
7: // You can run from the command line with:
8: // javaws Demo.jnlp
9: //
10: // (See tutorial at:
11: // http://docs.oracle.com/javase/tutorial/deployment/webstart/
12: // The JNLP API is documented at:
13: // http://docs.oracle.com/javase/7/docs/jre/api/javaws/jnlp/
14: // The JNLP (XML) file syntax is documented at:
15: // http://docs.oracle.com/javase/7/docs/technotes/guides/javaws/developersguide/syntax.html
16: // There are XML schema files for jnlp files, at:
17: // https://code.google.com/p/ivy-tools/source/browse/trunk/src/app/web/jnlp.xsd
18: // and:
19: // http://mindprod.com/jgloss/jnlp.html#XSDSCHEMAS (the latest one)
20:
21: package webstart;
22:
23: import java.awt.*;
24: import java.awt.event.*;
25: import java.io.*;
26: import javax.swing.*;
27: import javax.jnlp.*;
28: import static javax.swing.JFrame.*;
29:
30: public class Demo {
31: final static JFrame frame = new JFrame( "WebStartDemo" );
32: final static JPanel titlePanel = new JPanel();
33: final static JTextArea fileNameLabel = new JTextArea( "<No File Selected>" );
34: final static JPanel btnPanel = new JPanel();
35: final static JTextArea fileTextArea = new JTextArea();
36:
37: public static void main ( String [] args ) {
38: frame.setSize( 600, 400 );
39: frame.setLocationRelativeTo( null ); // Center on screen
40: frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
41:
42: // Draw the initial UI on the Event Dispatch Thread:
43: try {
44: SwingUtilities.invokeAndWait( new Runnable() {
45: public void run () {
46: drawUI( frame );
47: }
48: });
49: } catch ( Exception e ) {
50: e.printStackTrace();
51: System.exit( 1 );
52: }
53: frame.setVisible( true );
54: }
55:
56: private static void drawUI ( JFrame frame ) {
57: // Add and layout components:
58: frame.setLayout( new BorderLayout() );
59: frame.add( titlePanel, "North" );
60: fileNameLabel.setFont( new Font("SanSerif", Font.BOLD, 18 ) );
61: titlePanel.add( fileNameLabel );
62: frame.add( new JScrollPane( fileTextArea ), "Center" );
63: frame.add( btnPanel, "South" );
64: JButton btn = new JButton( "Open File..." );
65: btnPanel.add( btn );
66:
67: // Hook up Event handling:
68: btn.addActionListener( new ActionListener() {
69: public void actionPerformed ( ActionEvent ae ) {
70: displayFile();
71: }
72: });
73: }
74:
75: private static void displayFile () {
76: FileOpenService fos;
77:
78: // Get reference to JNLP (WebStart) file open service:
79: try {
80: fos = (FileOpenService) ServiceManager.lookup(
81: "javax.jnlp.FileOpenService" );
82: } catch ( UnavailableServiceException e ) {
83: fos = null;
84: }
85:
86: if ( fos != null ) {
87: try {
88: // Ask user to select a text file through JNLP service:
89: // (Note, JNLP may ignore the suggested extensions, so any
90: // file might be returned. TODO: Check file type is text.
91: FileContents fileContents =
92: fos.openFileDialog( null, new String[] { "txt", "java" } );
93:
94: // Update the file name label:
95: String fileName = fileContents.getName();
96: // JNLP may hide the file name, so:
97: if ( fileName == null || fileName == "" )
98: fileName = "<Unknown file name>";
99: fileNameLabel.setText( fileName );
100:
101: // Read and display file contents:
102: InputStreamReader isr =
103: new InputStreamReader( fileContents.getInputStream() );
104: fileTextArea.read( isr, null );
105: } catch ( Exception e ) {
106: e.printStackTrace();
107: }
108: }
109: }
110: }