Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

refresh jeditorpane

Status
Not open for further replies.

bateman23

Programmer
Mar 2, 2001
145
DE
I'm using a jeditorpane to display a html-file. (jEditorPane1.setPage(page);)
Then I'm changing the html-file through java-code, and want to refresh the jeditorpane with the new html. But the setPage-method doesn't work. The problem is also descibed there:

Any ideas how to solve the problem?

---------------------------------------
Visit me @:
 
//here is the a java browser
// java HtmlPanel to use it
// look at PageLoader
// I myself use linkActivated to load a new page but this is not a good habit
import java.io.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class HtmlPanel extends JPanel implements HyperlinkListener {
protected JEditorPane html;

public HtmlPanel(String startURL) {
setLayout(new BorderLayout());

try {
URL url = new URL(startURL);
html = new JEditorPane(url);
html.setEditable(false);
html.addHyperlinkListener(this);
JScrollPane scroller = new JScrollPane();
JViewport vp = scroller.getViewport();
vp.add(html);
vp.setBackingStoreEnabled(true);
add(scroller, BorderLayout.CENTER);

} catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e);
} catch (IOException e) {
System.out.println("IOException: " + e);
}
}

public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("myMess"+e.getURL());
linkActivated(e.getURL());
}
}

protected void linkActivated(URL u) {
Cursor c = html.getCursor();
Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
html.setCursor(waitCursor);
SwingUtilities.invokeLater(new PageLoader(u, c));
}

class PageLoader implements Runnable {
URL url;
Cursor cursor;

PageLoader(URL u, Cursor c) {
url = u;
cursor = c;
}
public void run() {
if (url == null) {
html.setCursor(cursor);
Container parent = html.getParent();
parent.repaint();
} else {
Document doc = html.getDocument();
try {
html.setPage(url);
} catch (IOException ioe) {
html.setDocument(doc);
getToolkit().beep();
} finally {
url = null;
SwingUtilities.invokeLater(this);
}
}
}
}

public static void main(String args[]) {
JFrame frame = new JFrame("Roadster");
HtmlPanel panel = new HtmlPanel(args[0]);

frame.getContentPane().add(panel);
frame.setSize(500, 300);
frame.setVisible(true);
}
}
 
Sorry, but that's impossible to use. i allways get "out of memory exceptions". Isn't there any possibility to get the jEditorPane updated..?

---------------------------------------
Visit me @:
 
//Please post your code here
// my modified version
import java.io.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.event.*;
public class myPan2 extends JPanel implements HyperlinkListener {
protected JEditorPane html;
URL url;
JButton goBut;
JTextField inputJT;
JPanel butPanel;
public myPan2() {
setLayout(new BorderLayout());

html=new JEditorPane("text/html","");
html.setText("<html><head></head><body><a href= to go to yahoo</a><li>Hello</li></body></html>");
html.setEditable(false);
html.addHyperlinkListener(this);
JScrollPane scroller = new JScrollPane();
JViewport vp = scroller.getViewport();
vp.add(html);
vp.setBackingStoreEnabled(true);
add(scroller, BorderLayout.CENTER);
goBut = new JButton("go");
inputJT = new JTextField(" butPanel = new JPanel();
butPanel.setLayout(new GridLayout(1,2));
add(butPanel, BorderLayout.SOUTH);
butPanel.add(inputJT);
butPanel.add(goBut);
goBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
inputJT.setText( (inputJT.getText()).trim() );
if ( (inputJT.getText()).length()>0 )
linkActivated( new URL(inputJT.getText()) );
}
catch (java.net.MalformedURLException me)
{System.out.println("MalformedURLException");}
}
});
}

public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
linkActivated(e.getURL());
}
}

protected void linkActivated(URL u) {
Cursor c = html.getCursor();
Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
html.setCursor(waitCursor);
SwingUtilities.invokeLater(new PageLoader(u, c));
}

class PageLoader implements Runnable {
URL url;
Cursor cursor;

PageLoader(URL u, Cursor c) {
url = u;
cursor = c;
}
public void run() {
if (url == null) {
html.setCursor(cursor);
Container parent = html.getParent();
parent.repaint();
} else {
Document doc = html.getDocument();
try {
html.setPage(url);
} catch (IOException ioe) {
html.setDocument(doc);
getToolkit().beep();
} finally {
url = null;
SwingUtilities.invokeLater(this);
}
}
}
}

public static void main(String args[]) {
JFrame frame = new JFrame("Roadster");
myPan2 myPanel = new myPan2();

frame.getContentPane().add(myPanel);
frame.setSize(500, 300);
frame.setVisible(true);
}
}
 
Hi,
i'm using the following code. It works for the first time, but not after the html-file has been changed:

File localFile = new File(htmlfile);
if(localFile.exists() && localFile.isFile()) {
String page = "file:///" + localFile.getAbsolutePath();
try {
this.jEditorPane1.setPage(page);
}
catch (Exception ee) {
}
}


---------------------------------------
Visit me @:
 
I've got the solution, thanx to cjard from codeguru!
setpage takes a URL, of the document to show, yet you state that you are changing the document? shouldnt you be using the getDocument() method and implementing your changes?

Despite i can't use getDocument() this hint gave me the clue.
Now i'm just loading a dummy file through setpage, before calling setpage again (this time with the file, which has been changed). That works...

But anyway. That can't be the best solution. Isn't there another one?

---------------------------------------
Visit me @:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top