7.2 KiB
7.2 KiB
Swing의 JEditorPane 및 EditorKit 정리
1. 주요 메서드 정리
(1) JEditorPane 메서드
| 메서드 | 설명 |
|---|---|
setContentType(String type) |
문서의 MIME 타입 설정 (text/plain, text/html, text/rtf 등) |
getContentType() |
현재 설정된 콘텐츠 타입 반환 |
setText(String text) |
편집기에 텍스트 설정 |
getText() |
현재 편집기의 텍스트 반환 |
setPage(String url) |
지정된 URL의 내용을 로드하여 표시 |
setPage(URL url) |
URL 객체를 이용하여 페이지 로드 |
read(Reader in, Object desc) |
입력 스트림을 이용하여 문서 로드 |
write(Writer out) |
현재 내용을 출력 스트림에 저장 |
setEditable(boolean b) |
편집 가능 여부 설정 |
isEditable() |
편집 가능 여부 확인 |
setEditorKit(EditorKit kit) |
특정 EditorKit을 사용하여 편집 동작 변경 |
getEditorKit() |
현재 사용 중인 EditorKit 반환 |
replaceSelection(String content) |
현재 선택된 텍스트를 새 텍스트로 대체 |
getSelectedText() |
현재 선택된 텍스트 반환 |
addHyperlinkListener(HyperlinkListener l) |
하이퍼링크 이벤트 리스너 추가 |
(2) EditorKit 메서드
| 메서드 | 설명 |
|---|---|
createDefaultDocument() |
새 기본 문서 생성 |
read(Reader in, Document doc, int pos) |
주어진 리더(Reader)에서 문서를 읽어옴 |
write(Writer out, Document doc, int pos, int len) |
문서를 지정된 범위 내에서 출력 스트림으로 저장 |
install(JEditorPane c) |
특정 JEditorPane에 EditorKit을 설치 |
deinstall(JEditorPane c) |
JEditorPane에서 EditorKit을 제거 |
getViewFactory() |
문서의 뷰를 생성하는 ViewFactory 반환 |
2. 관련 이벤트 정리
| 이벤트 리스너 | 관련 컴포넌트 | 설명 |
|---|---|---|
HyperlinkListener |
JEditorPane |
HTML 문서에서 하이퍼링크 클릭 시 발생 |
DocumentListener |
JEditorPane |
문서의 변경(삽입, 삭제 등)이 발생할 때 감지 |
CaretListener |
JEditorPane |
커서(캐럿) 위치가 변경될 때 발생 |
KeyListener |
JEditorPane |
키보드 입력을 감지 |
3. 예제 코드 및 설명
(1) 기본적인 JEditorPane 사용
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class JEditorPaneExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JEditorPane Example");
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/plain");
editorPane.setText("여기에 텍스트를 입력하세요.");
editorPane.setEditable(true);
JScrollPane scrollPane = new JScrollPane(editorPane);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
}
설명:
JEditorPane을 생성하고setContentType("text/plain")을 설정하여 일반 텍스트를 편집할 수 있도록 함.setEditable(true)로 설정하여 편집 가능하도록 함.JScrollPane을 사용하여 스크롤 가능하도록 추가.
(2) HTML 문서 표시 및 하이퍼링크 이벤트 처리
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.io.IOException;
public class JEditorPaneHyperlinkExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JEditorPane - HTML Example");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setText("<html><body>"
+ "<h1>HTML 지원</h1>"
+ "<p>이것은 <b>HTML</b> 형식의 문서입니다.</p>"
+ "<a href='https://www.example.com'>이 링크를 클릭하세요</a>"
+ "</body></html>");
editorPane.setEditable(false);
// 하이퍼링크 이벤트 처리
editorPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
editorPane.setPage(e.getURL()); // 링크 클릭 시 해당 페이지 로드
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
JScrollPane scrollPane = new JScrollPane(editorPane);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
}
설명:
JEditorPane을 생성하고setContentType("text/html")을 설정하여 HTML을 표시할 수 있도록 함.addHyperlinkListener()를 이용해 하이퍼링크 클릭 시 이벤트를 감지하여setPage(e.getURL())로 페이지를 로드하도록 함.setEditable(false)로 설정하여 사용자가 직접 편집하지 못하도록 함.
(3) EditorKit을 활용한 RTF 문서 편집
import javax.swing.*;
import javax.swing.text.rtf.RTFEditorKit;
import java.awt.*;
public class JEditorPaneRTFExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JEditorPane - RTF Example");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(new RTFEditorKit()); // RTF 전용 EditorKit 설정
editorPane.setText("{\\rtf1\\ansi This is RTF formatted text.}");
JScrollPane scrollPane = new JScrollPane(editorPane);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
}
설명:
RTFEditorKit을 사용하여 RTF(리치 텍스트 포맷) 문서를 편집할 수 있도록 설정.setEditorKit(new RTFEditorKit())을 통해JEditorPane에 적용.- RTF 포맷의 기본 문서를 설정하여 편집 가능하도록 함.
4. 총정리
JEditorPane:JTextPane과 유사하지만 HTML, RTF, 일반 텍스트 등 다양한 문서 형식을 지원.EditorKit: 특정 문서 포맷을 편집할 수 있도록 지원하는 클래스 (RTFEditorKit,HTMLEditorKit등).- 이벤트 처리:
HyperlinkListener→ HTML 문서에서 링크 클릭 감지.DocumentListener→ 문서 변경 감지.CaretListener→ 커서 이동 감지.
- 활용 예제:
- 일반 텍스트 편집기
- HTML 문서 뷰어 및 하이퍼링크 처리
- RTF 문서 편집기
위 내용을 활용하면 웹 기반 문서 편집기나 간단한 브라우저 기능을 구현할 수도 있다!