KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > SwingWTBrowser


1 /*
2  * SwingWT browser demo.
3  *
4  * @author R. Rawson-Tetley
5  *
6  * $Log: SwingWTBrowser.java,v $
7  * Revision 1.4 2003/12/14 08:47:37 bobintetley
8  * Added useful comments and CVS log header
9  *
10  */

11
12 package demo;
13
14 import swingwtx.swing.*;
15 import swingwtx.swing.event.*;
16 import swingwt.awt.*;
17 import swingwt.awt.event.*;
18
19 import java.net.*;
20 import java.io.*;
21
22 /**
23  * Very simplistic "Web browser" using SwingWT. Supply a URL on the
24  * command line to see it initially, and to set the destination
25  * of the "home" button.
26  */

27
28 public class SwingWTBrowser extends JFrame implements HyperlinkListener, ActionListener {
29   public static void main(String JavaDoc[] args) {
30     if (args.length == 0)
31       new SwingWTBrowser();
32     else
33       new SwingWTBrowser(args[0]);
34   }
35
36   private JButton homeButton;
37   private JTextField urlField;
38   private JEditorPane htmlPane;
39   private String JavaDoc initialURL;
40
41   public SwingWTBrowser() { this(""); }
42   
43   public SwingWTBrowser(String JavaDoc initialURL) {
44     super("Simple SwingWT Browser");
45     this.initialURL = initialURL;
46
47     JPanel topPanel = new JPanel();
48     topPanel.setLayout(new BorderLayout());
49     JPanel buttonPanel = new JPanel();
50     buttonPanel.setLayout(new FlowLayout());
51     topPanel.add(buttonPanel, BorderLayout.WEST);
52     
53     homeButton = new JButton();
54     homeButton.setIcon(new ImageIcon(getClass().getResource("/demo/Homepage.png")));
55     homeButton.addActionListener(this);
56     urlField = new JTextField(100);
57     urlField.setText(initialURL);
58     topPanel.add(urlField, BorderLayout.CENTER);
59     
60     urlField.addKeyListener(new KeyListener() {
61         public void keyPressed(KeyEvent k) {
62            if (k.getKeyCode() == KeyEvent.VK_ENTER) {
63                 String JavaDoc url = urlField.getText();
64                 try {
65                   htmlPane.setPage(new URL(url));
66                   urlField.setText(url);
67                 } catch(IOException ioe) {
68                   warnUser("Can't follow link to " + url + ": " + ioe);
69                 }
70             }
71         }
72         public void keyReleased(KeyEvent k) {}
73         public void keyTyped(KeyEvent k) {
74         }
75     });
76     
77     buttonPanel.add(homeButton);
78     
79     getContentPane().add(topPanel, BorderLayout.NORTH);
80
81     String JavaDoc sample = "<html><body><h2>Test HTML</h2><p>This is a simple browser test. Type in a URL at the top to go to a new location, or hit the home button to go back to the URL you supplied.</p></body></html>";
82     
83     try {
84         if (initialURL.equals(""))
85             htmlPane = new JEditorPane("text/html", sample);
86         else
87             htmlPane = new JEditorPane(initialURL);
88         htmlPane.setEditable(false);
89         htmlPane.addHyperlinkListener(this);
90         JScrollPane scrollPane = new JScrollPane(htmlPane);
91         getContentPane().add(scrollPane, BorderLayout.CENTER);
92     } catch(IOException ioe) {
93        warnUser("Can't build HTML pane for " + initialURL
94                 + ": " + ioe);
95     }
96
97     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
98     int width = screenSize.width * 8 / 10;
99     int height = screenSize.height * 8 / 10;
100     setBounds(width/8, height/8, width, height);
101     setVisible(true);
102     
103   }
104
105   public void actionPerformed(ActionEvent event) {
106     String JavaDoc url = initialURL;
107     if (url.equals("")) {
108         warnUser("You didn't supply an initial home URL!");
109         return;
110     }
111     try {
112       htmlPane.setPage(new URL(url));
113       urlField.setText(url);
114     } catch(IOException ioe) {
115       warnUser("Can't follow link to " + url + ": " + ioe);
116     }
117   }
118
119   public void hyperlinkUpdate(HyperlinkEvent event) {
120     if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
121       try {
122         htmlPane.setPage(event.getURL());
123         urlField.setText(event.getURL().toExternalForm());
124       } catch(IOException ioe) {
125         warnUser("Can't follow link to "
126                  + event.getURL().toExternalForm() + ": " + ioe);
127       }
128     }
129   }
130
131   private void warnUser(String JavaDoc message) {
132     JOptionPane.showMessageDialog(this, message, "Error",
133                                   JOptionPane.ERROR_MESSAGE);
134   }
135 }
136     
137     
138
Popular Tags