Hướng dẫn sử dụng JEditorPane trong Java Swing

JEditorPane là một text component có thể xử lý các dữ liệu thuộc nhiều dạng văn bản như plain text, HTML và Rich Text Format (RTF).

JEditorPane được sử dụng chủ yếu để hiển thị các tài liệu dạng HTML, nhưng một hạn chế của nó là chỉ xử lý được các thành phần HTML đơn giản.

Đoạn code sau mô tả cách hiển thị một trang HTML 

JEditorPane htmlPane  = new JEditorPane("https://shareprogramming.net/");

Mặc định JEditorPane có thể hiểu được các nội dung ở dạng:

  • text/plain
  • text/html
  • text/rtf

Ví dụ sử dụng JEditorPane để hiển thị trang web 

import java.awt.BorderLayout;
import java.awt.Container;
import java.beans.PropertyChangeEvent;
import java.io.IOException;
import java.net.URL;
import javax.swing.Box;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;

public class Main extends JFrame {
    JEditorPane editorPane = new JEditorPane();

    public Main(String title) {
        super(title);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = this.getContentPane();
        Box editorPaneBox = this.getEditPaneBox();
        contentPane.add(editorPaneBox, BorderLayout.CENTER);
    }
    private Box getEditPaneBox() {
        editorPane.setEditable(false);
        Box editorBox = Box.createHorizontalBox();
        editorBox.add(new JScrollPane(editorPane));

        editorPane.addHyperlinkListener((HyperlinkEvent event) -> {
            if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                go(event.getURL());
            } else if (event.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                System.out.println("click this link");
            } else if (event.getEventType() == HyperlinkEvent.EventType.EXITED) {
                System.out.println("Ready");
            }
        });

        editorPane.addPropertyChangeListener((PropertyChangeEvent e) -> {
            String propertyName = e.getPropertyName();
            if (propertyName.equalsIgnoreCase("page")) {
                URL url = editorPane.getPage();
                System.out.println(url.toExternalForm());
            }
        });

        return editorBox;
    }
    public void go(URL url) {
        try {
            editorPane.setPage(url);

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
    public void go(String urlString) {
        try {
            URL url = new URL(urlString);
            go(url);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
    public static void main(String[] args) {
        Main browser = new Main("");
        browser.setSize(700, 500);
        browser.setVisible(true);
        browser.go("https://www.w3schools.com/");
    }
}

Output

JEditorPane

Như các bạn thấy thì đối với các trang web hiện tại thì JEditorPane không thể hiển thị đầy đủ nội dung, nên nó cũng ít được sử dụng.

Nguồn

http://www.java2s.com/Tutorials/Java/Java_Swing/0860__Java_Swing_JEditorPane.htm

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x