1
2 package samplecode;
3
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 /**
9 * <p>Title: Execute Sample</p>
10 * <p>Description: Thip program shows how to execute a program outside of
11 * java </p>
12 * <p>Company: Developer's super Page.com</p>
13 */
14
15
16 public class SampleFrame extends JFrame {
17 JPanel contentPane;
18 BorderLayout borderLayout1 = new BorderLayout();
19 JPanel jpnlMain = new JPanel();
20 GridBagLayout gridBagLayout1 = new GridBagLayout();
21 JButton btnExecute = new JButton();
22
23 //Construct the frame
24 public SampleFrame() {
25 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
26 try {
27 jbInit();
28 }
29 catch(Exception e) {
30 e.printStackTrace();
31 }
32 }
33
34 //Component initialization
35 private void jbInit() throws Exception {
36 contentPane = (JPanel) this.getContentPane();
37 contentPane.setLayout(borderLayout1);
38 this.setSize(new Dimension(400, 300));
39 this.setTitle("Execute Application");
40 jpnlMain.setLayout(gridBagLayout1);
41 contentPane.setMinimumSize(new Dimension(400, 300));
42 jpnlMain.setMinimumSize(new Dimension(400, 256));
43 jpnlMain.setPreferredSize(new Dimension(400, 256));
44 btnExecute.setText("Execute");
45 btnExecute.addActionListener(new SampleFrame_btnExecute_actionAdapter(this));
46 contentPane.add(jpnlMain, BorderLayout.CENTER);
47 jpnlMain.add(btnExecute, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
48 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, -10,
49 0, 10), 0, 0));
50 }
51
52 //Overridden so we can exit when window is closed
53 protected void processWindowEvent(WindowEvent e) {
54 super.processWindowEvent(e);
55 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
56 System.exit(0);
57 }
58 }
59
60 void btnExecute_actionPerformed(ActionEvent e) {
61 /* this function execute notepad and if not successfull it
62 will display an error message to the user */
63
64 try
65 {
66 //Execute notpad
67 Runtime.getRuntime().exec("C:\\winnt\\Notepad.exe");
68 }
69 catch(Exception ioe)
70 {
71 //Display Error Message
72 JOptionPane Pane = new JOptionPane();
73 Pane.showMessageDialog(null, ioe.toString(),
74 "Error",
75 JOptionPane.ERROR_MESSAGE);
76 }
77
78 }
79 }
80
81 class SampleFrame_btnExecute_actionAdapter implements java.awt.event.ActionListener
82 {
83 SampleFrame adaptee;
84
85 SampleFrame_btnExecute_actionAdapter(SampleFrame adaptee) {
86 this.adaptee = adaptee;
87 }
88 public void actionPerformed(ActionEvent e) {
89 adaptee.btnExecute_actionPerformed(e);
90 }
91 }
|