An Example
You now create your first JNLP file that will launch a dummy application over the Web. If you don't have a Web server, you can modify the example in order to let it work from the local file system as well. The JNLP file in Listing 1 did that. Specifically, see line 3 for an example of how to use the local file system with JNLP.
The Application
Suppose you have an application, a common Java application that you have created before you knew about JNLP. The application is reported in Listing 2.
Listing 2. A Simple Example Application
00 import java.awt.*; 01 import javax.swing.*; 02 import java.awt.event.*; 03 04 /** 05 * An Example Application 06 * "Hello JNLP!" 07 * Just pops up a dialog 08 * 09 * @author Mauro Marinilli 10 * @version 1.0 11 */ 12 public class Example extends JDialog { 13 JPanel panel1 = new JPanel(); 14 BorderLayout borderLayout1 = new BorderLayout(); 15 JLabel jLabel = new JLabel(); 16 JPanel southPanel = new JPanel(); 17 JButton okButton = new JButton(); 18 19 public Example(Frame frame, String title, boolean modal) { 20 super(frame, title, modal); 21 try { 22 initUI(); 23 pack(); 24 } catch(Exception ex) { 25 ex.printStackTrace(); 26 } 27 } 28 29 public Example() { 30 this(null, "", true); 31 } 32 33 void initUI() throws Exception { 34 panel1.setLayout(borderLayout1); 35 jLabel.setHorizontalAlignment(SwingConstants.CENTER); 36 jLabel.setHorizontalTextPosition(SwingConstants.CENTER); 37 jLabel.setLabelFor(okButton); 38 jLabel.setText("<html><body><h1>Hello JNLP!"); 39 okButton.setToolTipText("close the example dialog"); 40 okButton.setText("<html><body><tt>OK"); 41 okButton.addActionListener(new java.awt.event.ActionListener() { 42 public void actionPerformed(ActionEvent e) { 43 dismiss(); 44 } 45 }); 46 setTitle("Example Dialog"); 47 addWindowListener(new java.awt.event.WindowAdapter() { 48 public void windowClosing(WindowEvent e) { 49 dismiss(); 50 } 51 }); 52 getContentPane().add(panel1); 53 panel1.add(jLabel, BorderLayout.NORTH); 54 panel1.add(southPanel, BorderLayout.SOUTH); 55 southPanel.add(okButton, null); 56 pack(); 57 setVisible(true); 58 } 59 60 /** 61 * the Main method 62 */ 63 static public void main(String[] args) { 64 Example e = new Example(); 65 } 66 67 /** 68 * dismiss the dialog 69 */ 70 void dismiss() { 71 System.exit(0); 72 } 73 74 } 75
This is just an example that shows a message dialog box.