Help with JEditorPane

Hello,I am currently making a simple web browser that uses JEditorPane to render websites.I'm trying to make a stop-button, but have no idea on how to get started. Can anyone point me in the right direction?Thanks,Max Pagels
[260 byte] By [meapagelsa] at [2007-11-26 18:08:31]
# 1
there is a great tutorial for swing at http://java.sun.com/docs/books/tutorial/ui/index.html
suparenoa at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for the link, but i have everything already working except the stop button and would specifically need help on how to implement it.
meapagelsa at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 3

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

glevnera at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 4
Geoff: setText(null) is exactly what i'm looking for: I'm still a n00b, so i'm not trying anything overly fancy.Thank You!!!!!
meapagelsa at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 5
what do you except when the app do: setText(null)?
suparenoa at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 6

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);

}

}

meapagelsa at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 7
If you need a WebBrowser in Java, let your hand away from JEditorPane you will never be lucky!try https://jdic.dev.java.net/
Craven01a at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 8
This is a school assignment and JEditorPane has to be used. So any ideas?
meapagelsa at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 9

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).

glevnera at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 10
Is I said, earlier, I'm am completely new to Java and have no experience with threads.....
meapagelsa at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...
# 11
> Is I said, earlier, I'm am completely new to Java and> have no experience with threads.....so read tutorials http://java.sun.com/docs/books/tutorial/uiswing/ http://java.sun.com/docs/books/tutorial/uiswing/components/text.html...
suparenoa at 2007-7-9 5:40:12 > top of Java-index,Java Essentials,Java Programming...