Bitcoin

Bitcoin
Bitcoin

JEditorPane Java

JEditorPane is a kind of text area which can display various text formats. By default, JEditorPane supports HTML and RTF (Rich Text Format). However you can build your own “editor kits” to handle specific content type. You can use setContentType() method to choose document you want to display and setEditorKit() method to set custom editor for JEditorPane explicitly.
In practice, JEditorPane is typically used for displaying HTML only.  JEditorPane also supports RTF but very limited. You can set content for JEditorPane in the following ways:
  • Pass URL object or URL string into constructor of JEditorPane.
  • Use setPage() method to set content of JEditorPane at runtime.
  • Pass content as a String to the setText() method.
  • Use read() and supplying an HTMLDocument object with InputStream object.
To make JEditorPane read only, you use method setEditable(false).  When displaying HTML document, JEditorPane can detect HTML links and which link user clicks. To handle  the click event on links, you need to handle HyperlinkListener event.

Example of using JEditorPane to create a simple web browser

Sample programs listed in this section have been tested with JDK 1.6.0 to 1.8.0.
import javax.swing.*;
public class JEditorPaneTest {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new JEditorPaneTest()).test();
   }
   private void test() {
      myFrame = new JFrame("JEditorPane Test");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myFrame.setSize(300,200);
      
      JEditorPane myPane = new JEditorPane();
      myPane.setContentType("text/plain");
      myPane.setText(
         "JEditorPane is a text component to edit various kinds of"
         +" content.\n\nThis component uses implementations of the"
         +" EditorKit to accomplish its behavior.");

      myFrame.setContentPane(myPane);
      myFrame.setVisible(true);
   }
}
If you run this example, you will see a text editor pane displayed with the initial text content.
You can edit initial text or add more text. You can also select a part of the text as shown in the picture below:
Editor Pane Test

OTHER EXAMPLES
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.io.IOException; 
public class JavaExampleEditorPaneHTMLFormInJFrame extends JFrame 
 { 
       JEditorPane EdtrPn = new JEditorPane(); 
       public JavaExampleEditorPaneHTMLFormInJFrame() 
       { 
           super("Example Of Pane Editor HTML Form In Java Swing"); 
           Container Cntnr = getContentPane(); 
           EdtrPn.setEditable(false); 
           String URL = "file:" + System.getProperty("user.dir") + 
System.getProperty("file.separator")+"page.html"; 
            try 
               { 
                  EdtrPn.setPage(URL); 
               } 
                 catch(IOException e) {} 
                 Cntnr.add(EdtrPn); 
        } 
               public static void main(String as[]) 
               { 
                   final JFrame frm = new JavaExampleEditorPaneHTMLFormInJFrame(); 
                   frm.setBounds(110, 110, 310, 310); 
                   frm.setVisible(true); 
                   frm.setBackground(Color.White); 
                   frm.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
                   frm.addWindowListener(new WindowAdapter() 
                { 
                       public void windowClosing(WindowEvent e1) 
                     { 
                             System.exit(0); 
                         } 
                    }); 
                } 
  } 
      
THE OUTPUT
Java JEditorPane HTML Form Example
In this example, we will create a very simple web browser to display any web page by using JEditorPane component. In this example, we are also using JButton and JTextField components.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
public class Main {
    public static void main(String[] args) {
        final JFrame frame = new JFrame("JEditorPane Demo");
        String initialURL = "http://www.google.com/";
        final JEditorPane ed;
        JLabel lblURL = new JLabel("URL");
        final JTextField txtURL = new JTextField(initialURL, 30);
        JButton btnBrowse = new JButton("Browse");
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(lblURL);
        panel.add(txtURL);
        panel.add(btnBrowse);
        try {
            ed = new JEditorPane(initialURL);
            ed.setEditable(false);
            ed.addHyperlinkListener(new HyperlinkListener() {
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                        JEditorPane pane = (JEditorPane) e.getSource();
                        if (e instanceof HTMLFrameHyperlinkEvent) {
                            HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
                            HTMLDocument doc = (HTMLDocument) pane.getDocument();
                            doc.processHTMLFrameHyperlinkEvent(evt);
                        } else {
                            try {
                                pane.setPage(e.getURL());
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    }
                }
            });
            btnBrowse.addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            try {
                                ed.setPage(txtURL.getText().trim());
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
            JScrollPane sp = new JScrollPane(ed);
            frame.setLayout(new BorderLayout());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(panel, BorderLayout.NORTH);
            frame.getContentPane().add(sp, BorderLayout.CENTER);
            frame.setSize(500, 350);
            frame.setVisible(true);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

No comments:

Post a Comment

Facebook