Re: Is it possible to have a java program to show multiple browser?
In article
<c8f5e2f3-a764-408a-a996-61e866ccd590@h2g2000yqg.googlegroups.com>,
Newbie <aznch1239@gmail.com> wrote:
Is it possible to have a [J]ava program to show multiple browser[s]
on one screen, as on a security camera monitor screen?
Yes, for example:
<code>
package test.my.gui;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.IOException;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
/** @author John B. Matthews */
public class HtmlView extends JPanel implements HyperlinkListener {
private static final String EXAMPLE = "http://www.example.com";
private final JEditorPane jep;
private final Document doc;
public HtmlView(String url) {
this.setLayout(new GridLayout(1, 1));
jep = new JEditorPane();
jep.setEditable(false);
jep.addHyperlinkListener(this);
loadPage(jep, url);
doc = jep.getDocument();
JScrollPane sp = new JScrollPane(jep);
sp.setPreferredSize(new Dimension(800, 200));
sp.getVerticalScrollBar().setUnitIncrement(16);
this.add(sp);
}
private void loadPage(JEditorPane jep, String name) {
try {
jep.setPage(name);
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
HyperlinkEvent.EventType type = e.getEventType();
final URL url = e.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("URL: " + url);
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Following link...");
try {
jep.setPage(url);
} catch (IOException ioException) {
System.err.println("Invalid link");
jep.setDocument(doc);
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(3, 1));
f.add(new HtmlView(EXAMPLE));
f.add(new HtmlView(EXAMPLE));
f.add(new HtmlView(EXAMPLE));
f.pack();
f.setVisible(true);
}
});
}
}
</code>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>