V
VisionSet
This demo app loads a bunch of URLs into JEditorPanes in a JTabbedPane.
I don't think HTML engine in the JRE is much cop, so this is probably a
non-starter.
But I'd at least expect my interface to remain responsive. Is my code broken
or Suns?
I'm guessing the renderer is just consuming tOo much resources.
TIA
Mike W
compilable example:
import java.net.URL;
import java.io.IOException;
import java.net.MalformedURLException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.beans.*;
public class GuiFrame extends JFrame {
private JTabbedPane tabbedPane;
private int idx;
public GuiFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane();
tabbedPane.setPreferredSize(new Dimension(600,500));
add(tabbedPane, BorderLayout.CENTER);
JPanel northPanel = new JPanel(
new FlowLayout(FlowLayout.CENTER));
JButton searchButton = new JButton("Load some pages");
searchButton.addActionListener(searchAction);
northPanel.add(searchButton);
add(northPanel, BorderLayout.NORTH);
pack();
setVisible(true);
}
private ActionListener searchAction = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
search();
}
});
}
};
private URL[] someUrls() {
try {
return new URL[] {
new URL("http://openp2p.com"),
new URL("http://www.p2pnet.net"),
new URL("http://en.wikipedia.org/wiki/Peer-to-peer"),
new URL("http://www.limewire.com/english/content/home.shtml"),
new URL("http://morpheus.com"),
new URL("http://p2p.wrox.com"),
new URL("http://www.slyck.com"),
new URL("http://p2p.weblogsinc.com"),
new URL("http://p2p-politics.org")
};
}
catch(MalformedURLException ex) {
ex.printStackTrace();
return new URL[0];
}
}
private void search() {
URL[] urls = someUrls();
tabbedPane.removeAll();
idx = 0;
for (final URL url : urls) {
System.out.println("adding: " + url);
Thread pageLoader = new Thread(new Runnable() {
public void run() {
PageView view = null;
try {
view = new PageView(url);
}
catch(IOException ex) {
return;
}
tabbedPane.addTab(
Integer.toString(++idx), view);
tabbedPane.revalidate();
}
});
pageLoader.setPriority(Thread.MIN_PRIORITY);
pageLoader.start();
}
}
public static void main(String[] args) {
new GuiFrame();
}
}
class PageView extends JPanel {
private final JScrollPane scrollPane = new JScrollPane();
private final JEditorPane textComp = new JEditorPane();
public PageView(URL url) throws IOException {
setLayout(new BorderLayout());
textComp.setContentType("text/html");
textComp.setPage(url);
textComp.setEditable(false);
add(scrollPane);
Document doc = textComp.getDocument();
if (doc instanceof AbstractDocument) {
((AbstractDocument)doc).setAsynchronousLoadPriority(+1);
}
textComp.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("page")) {
if(scrollPane.getViewport().getView() != null) return;
scrollPane.setViewportView(textComp);
revalidate();
}
}
});
textComp.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent evt) {
try{
if(HyperlinkEvent.EventType.ACTIVATED
== evt.getEventType()) textComp.setPage(evt.getURL());
}
catch(IOException ex) {
ex.printStackTrace();
}
}
});
}
}
I don't think HTML engine in the JRE is much cop, so this is probably a
non-starter.
But I'd at least expect my interface to remain responsive. Is my code broken
or Suns?
I'm guessing the renderer is just consuming tOo much resources.
TIA
Mike W
compilable example:
import java.net.URL;
import java.io.IOException;
import java.net.MalformedURLException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.beans.*;
public class GuiFrame extends JFrame {
private JTabbedPane tabbedPane;
private int idx;
public GuiFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane();
tabbedPane.setPreferredSize(new Dimension(600,500));
add(tabbedPane, BorderLayout.CENTER);
JPanel northPanel = new JPanel(
new FlowLayout(FlowLayout.CENTER));
JButton searchButton = new JButton("Load some pages");
searchButton.addActionListener(searchAction);
northPanel.add(searchButton);
add(northPanel, BorderLayout.NORTH);
pack();
setVisible(true);
}
private ActionListener searchAction = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
search();
}
});
}
};
private URL[] someUrls() {
try {
return new URL[] {
new URL("http://openp2p.com"),
new URL("http://www.p2pnet.net"),
new URL("http://en.wikipedia.org/wiki/Peer-to-peer"),
new URL("http://www.limewire.com/english/content/home.shtml"),
new URL("http://morpheus.com"),
new URL("http://p2p.wrox.com"),
new URL("http://www.slyck.com"),
new URL("http://p2p.weblogsinc.com"),
new URL("http://p2p-politics.org")
};
}
catch(MalformedURLException ex) {
ex.printStackTrace();
return new URL[0];
}
}
private void search() {
URL[] urls = someUrls();
tabbedPane.removeAll();
idx = 0;
for (final URL url : urls) {
System.out.println("adding: " + url);
Thread pageLoader = new Thread(new Runnable() {
public void run() {
PageView view = null;
try {
view = new PageView(url);
}
catch(IOException ex) {
return;
}
tabbedPane.addTab(
Integer.toString(++idx), view);
tabbedPane.revalidate();
}
});
pageLoader.setPriority(Thread.MIN_PRIORITY);
pageLoader.start();
}
}
public static void main(String[] args) {
new GuiFrame();
}
}
class PageView extends JPanel {
private final JScrollPane scrollPane = new JScrollPane();
private final JEditorPane textComp = new JEditorPane();
public PageView(URL url) throws IOException {
setLayout(new BorderLayout());
textComp.setContentType("text/html");
textComp.setPage(url);
textComp.setEditable(false);
add(scrollPane);
Document doc = textComp.getDocument();
if (doc instanceof AbstractDocument) {
((AbstractDocument)doc).setAsynchronousLoadPriority(+1);
}
textComp.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("page")) {
if(scrollPane.getViewport().getView() != null) return;
scrollPane.setViewportView(textComp);
revalidate();
}
}
});
textComp.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent evt) {
try{
if(HyperlinkEvent.EventType.ACTIVATED
== evt.getEventType()) textComp.setPage(evt.getURL());
}
catch(IOException ex) {
ex.printStackTrace();
}
}
});
}
}