forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson39.java
More file actions
185 lines (112 loc) · 3.92 KB
/
JavaLesson39.java
File metadata and controls
185 lines (112 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
// Thrown when a URL doesn't contain http://
// and other rules like that
import java.net.MalformedURLException;
import java.net.URL;
// A text component that allows for rich text
// and basic html display
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
// Provides information on events triggered
// through interaction with links
import javax.swing.event.HyperlinkEvent;
// Monitors user activity with links
import javax.swing.event.HyperlinkListener;
public class JavaLesson39 extends JFrame implements HyperlinkListener, ActionListener{
public static void main(String[] args){
new JavaLesson39("file:///Volumes/My%20Book/Presentations/HTML%20Tutorial/htmlexample.html");
}
String defaultURL;
JPanel toolPanel = new JPanel();
JTextField theURL = new JTextField(25);
// Displays basic html pages
// Doesn't understand JavaScript
JEditorPane htmlPage;
public JavaLesson39(String defaultURL){
JFrame frame = new JFrame("Java Browser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.defaultURL = defaultURL;
// If the user interacts with the JTextField the
// actionPerformed method is called
theURL.addActionListener(this);
// Set default text in the JTextField
theURL.setText(defaultURL);
// Add the text field to a panel
toolPanel.add(theURL);
// Add the panel to the northern quadrant of a frame
frame.add(toolPanel, BorderLayout.NORTH);
try {
htmlPage = new JEditorPane(defaultURL);
// If the user interacts with the JEditorPane
// actions are triggered. Ex. Click on a link
// change the JEditorPane page location
htmlPage.addHyperlinkListener(this);
// You can leave this true for rich text documents
// but it will mess up web page display
htmlPage.setEditable(false);
// Add the JEditorPane to a Scroll pane
JScrollPane scroller = new JScrollPane(htmlPage);
// Add Scroll pane and JEditorPane to the frame
frame.add(scroller, BorderLayout.CENTER);
}
// If something goes wrong with locating the html page
// this will handle that error
catch (IOException e) {
e.printStackTrace();
}
// Set size of the frame and display it
frame.setSize(1200, 800);
frame.setVisible(true);
}
public void hyperlinkUpdate(HyperlinkEvent e) {
// Checks if a link was clicked
// EventType.ENTERED : Checks for hovering
// EventType.EXITED : Checks for leaving link
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
try {
// Sets the URL to be displayed
// getURL gets the URL for the link
htmlPage.setPage(e.getURL());
// toExternalForm creates a String representation of the URL
theURL.setText(e.getURL().toExternalForm());
}
catch(IOException e1){
e1.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
String pageURL = "";
// Gets the Object that had an event triggered
if(e.getSource() == theURL){
// Get the text in the JTextField
pageURL = theURL.getText();
} else {
pageURL = defaultURL;
// Opens an alert box when an error is made
JOptionPane.showMessageDialog(JavaLesson39.this,
"Please Enter a Web Address", "Error",
JOptionPane.ERROR_MESSAGE);
}
try{
// Sets the URL to be displayed
htmlPage.setPage(new URL(pageURL));
theURL.setText(pageURL);
}
catch(MalformedURLException e2){
JOptionPane.showMessageDialog(JavaLesson39.this,
"Please use http://", "Error",
JOptionPane.ERROR_MESSAGE);
}
catch(IOException e1){
e1.printStackTrace();
}
}
}