Sunday, December 14, 2008

SWING(EditorPane)

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.text.html.*;
import javax.swing.event.*;
import java.net.*;

public class EditorPaneTest extends JFrame implements HyperlinkListener,ActionListener{
JEditorPane ept;
JTextField jtf = new JTextField(" Type URL Here... ",30);
JButton click = new JButton(" Go ");
JPanel jp = new JPanel(new FlowLayout());
URL u;
public EditorPaneTest(){
jtf.selectAll();
jp.add(jtf);
click.addActionListener(this);
jp.add(click);
try{
ept = new JEditorPane("http://www.google.com");
ept.addHyperlinkListener(this);
}
catch(Exception e){
e.printStackTrace();
}
getContentPane().add("North",jp);
getContentPane().add(ept);
ept.setEditable(false);
show();
setSize(400,400);
}
public void hyperlinkUpdate(HyperlinkEvent hle){
System.out.println("clicked");

/*if (hle instanceof HTMLFrameHyperlinkEvent)
try{
HTMLFrameHyperlinkEvent e = (HTMLFrameHyperlinkEvent)hle;
HTMLDocument dcmt = (HTMLDocument)ept.getDocument();
dcmt.processHTMLFrameHyperlinkEvent(e);
//ept.setPage("http://localhost:8080/");
}
catch(Exception e){
e.printStackTrace();
}

else{*/
try{
ept.setPage(hle.getURL());
jtf.setText(hle.getURL().toString());
}catch(Exception ee){
ee.printStackTrace();
}
}
String urlString = "http://209.85.153.104";
public void actionPerformed(ActionEvent ae){
System.out.println("Clicked");
urlString = jtf.getText();
try{
u = new URL(urlString);
ept.setPage(u);
ept.updateUI();
}catch(Exception eee){}
}
public static void main(String args[]){
new EditorPaneTest();
}
}

No comments:

Post a Comment