What exactly is it about the stop button that makes it difficult for you?
Is your question about how to interrupt the loading of the URL? If so, then the simplest way is for you to just clear the JEditorPane when the user clicks on the stop button, i.e. by calling setText(null). If you want to do something more complicated, you may have to subclass JEditorPane and override the getStream() method, if you want to be able to intervene directly on the input stream.
Geoff
so, i' going to try setText(null) but doesn't this require the use of threads? And if so, can anyone tell me how to get started?
Here is the code i have so far:
import java.awt.event.*;
import java.io.IOException;
import org.jdesktop.layout.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class Browser extends JFrame implements ActionListener,
HyperlinkListener {
private JButton backButton;
private JComboBox bookmarksBox;
private JLabel bookmarksLabel;
private JButton forwardButton;
private JButton homeButton;
private JEditorPane htmlPane;
private JScrollPane scrollPanel;
private JButton stopButton;
private JTextField urlField;
private GroupLayout layout;
private Vector<URL> forwardURLs;
private Vector<URL> backURLs;
private String homepage;
/** Creates a new instance of the Browser class */
public Browser() {
super("Browser v1.0");
initComponents();
}
private void initComponents() {
backButton = new JButton();
bookmarksBox = new JComboBox();
bookmarksLabel = new JLabel();
forwardButton = new JButton();
homeButton = new JButton();
htmlPane = new JEditorPane();
scrollPanel = new JScrollPane();
stopButton = new JButton();
urlField = new JTextField();
forwardURLs = new Vector<URL>();
backURLs = new Vector<URL>();
homepage = "http://www.pocketoptimized.com/pda";
bookmarksLabel.setText("Bookmarks:");
bookmarksBox.addActionListener(this);
bookmarksBox.setModel(new DefaultComboBoxModel(new String[] {
"http://www.pocketoptimized.com/pda",
"http://news.bbc.co.uk/2/low/",
"http://www.google.com/xhtml"}));
backButton.setText("Back");
backButton.addActionListener(this);
forwardButton.setText("Forward");
forwardButton.addActionListener(this);
homeButton.setText("Home");
homeButton.addActionListener(this);
stopButton.setText("Stop");
stopButton.addActionListener(this);
htmlPane.setEditable(false);
htmlPane.addHyperlinkListener(this);
scrollPanel.setViewportView(htmlPane);
urlField.setEditable(true);
urlField.addActionListener(this);
try {
htmlPane.setPage(homepage);
urlField.setText(homepage);
}
catch (IOException e) {
warnUser();
}
layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(urlField, 0, 550, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(homeButton)
.addPreferredGap(LayoutStyle.RELATED)
.add(backButton)
.addPreferredGap(LayoutStyle.RELATED)
.add(forwardButton)
.addPreferredGap(LayoutStyle.RELATED)
.add(stopButton)
.add(12, 12, 12)
.add(bookmarksLabel)
.addPreferredGap(LayoutStyle.RELATED)
.add(bookmarksBox, 0, 210, Short.MAX_VALUE))
.add(scrollPanel, GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(urlField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.RELATED)
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(homeButton)
.add(backButton)
.add(forwardButton)
.add(stopButton)
.add(bookmarksLabel)
.add(bookmarksBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.RELATED)
.add(scrollPanel, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))
);
pack();
}
/** Application logic for when a specific action is performed in the
* Browser GUI, including attempts to display URI and update
* information stored for forward/back button functionality */
public void actionPerformed(ActionEvent event) {
if (event.getSource() == urlField) {
String url;
/* Protocol check */
if (urlField.getText().length() == 0)
return;
else if (!urlField.getText().startsWith("http://"))
url = "http://" + urlField.getText();
else
url = urlField.getText();
backURLs.add(htmlPane.getPage());
try {
htmlPane.setPage(url);
}
catch (IOException e) {
warnUser();
backURLs.removeElementAt(backURLs.size()-1);
}
forwardURLs.removeAllElements();
}
else if (event.getSource() == backButton) {
if (backURLs.isEmpty())
return;
else {
forwardURLs.add(htmlPane.getPage());
try {
htmlPane.setPage(backURLs.lastElement());
urlField.setText(backURLs.lastElement().toString());
}
catch (IOException e) {
warnUser();
forwardURLs.removeElementAt(backURLs.size()-1);
}
backURLs.removeElementAt(backURLs.size()-1);
}
}
else if (event.getSource() == forwardButton) {
if (forwardURLs.isEmpty())
return;
else {
backURLs.add(htmlPane.getPage());
try {
htmlPane.setPage(forwardURLs.lastElement());
urlField.setText(forwardURLs.lastElement().toString());
}
catch (IOException e) {
warnUser();
backURLs.removeElementAt(backURLs.size()-1);
}
forwardURLs.removeElementAt(forwardURLs.size()-1);
}
}
else if (event.getSource() == homeButton) {
try {
htmlPane.setPage(homepage);
urlField.setText(homepage);
backURLs.removeAllElements();
forwardURLs.removeAllElements();
}
catch (IOException e) {
warnUser();
}
}
else if (event.getSource() == stopButton) {
htmlPane.setText(null);
urlField.setText("blank");
}
else if (event.getSource() == bookmarksBox) {
backURLs.add(htmlPane.getPage());
try {
htmlPane.setPage(bookmarksBox.getSelectedItem().toString());
urlField.setText(bookmarksBox.getSelectedItem().toString());
}
catch (IOException e) {
warnUser();
backURLs.removeElementAt(backURLs.size()-1);
}
forwardURLs.removeAllElements();
}
}
/** hyperlinkUpdate is initiated if a hyperlink is selected:
the Browser class attempts to show the URL in question & update
information stored for forward/back button functionality */
public void hyperlinkUpdate(HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
backURLs.add(htmlPane.getPage());
try {
htmlPane.setPage(event.getURL());
urlField.setText(event.getURL().toString());
}
catch (IOException e) {
warnUser();
backURLs.removeElementAt(backURLs.size()-1);
}
forwardURLs.removeAllElements();
}
}
private void warnUser() {
JOptionPane.showMessageDialog(this,
"It seems the URL you provided cannot be reached.\n" +
"Please check the spelling of the URL in question, " +
"try again later, or panic.",
"Noooo!",
JOptionPane.INFORMATION_MESSAGE);
}
/** Creates a new instance of the Browser class, sets the visibility
* to true and applies logic for closing the application (JFrame.EXIT_ON_CLOSE) */
public static void main(String args[]) {
Browser browser = new Browser();
browser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
browser.setVisible(true);
}
}
I don't feel much like reading all your code, especially without code tags. But offhand I don't see why you are concerned about threads. Just be sure to call the setText() method from the event dispatching thread, which will always be the case if you do it in an action listener (the method invoked by Swing when your stop button is pushed).