How to use JEditor in Java, Lecture notes of Object Oriented Programming

My self made helping mannual notes on JEditor

Typology: Lecture notes

2020/2021

Uploaded on 07/01/2021

soul-recto
soul-recto 🇵🇰

5

(1)

6 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JEDITOR Class (JAVA)
1. OBJECTIVES:
Since this tutorial has been designed for novices to help them learn the fundamental
functioning of JAVA Swing , Jeditor Pane and their structure in Java, the primary goals of
this research effort are as follows:
To understand what JEditor Pane is?
To understand the difference between JText Area and JEditor.
To understand the practical work implementation in JEditor.
To understand the purpose of using JEditor and its Applications.
To understand JEditor Coding for programming.
2. CLASS NAME:
The class name for this tutorials discussion is “JEditorpane.”
3. INTRODUCTION:
A JEditorPane is a text component that can handle different text with style. By
default, it can handle plain text, HTML, and Rich Text Format (RTF). A
JEditorPane is primarily used to display an HTML document with only basic
HTML elements.
This class has setContentType() and setText() methods.
setContentType("text/plain"): This method is used to set the content
type to be plain text.
setText(text): This method is used to set the initial text content.
However, we can build your own “editor kits” to handle specific content type. We can
use setContentType() method to choose the document we want to display and setEditorKit()
method to set custom editor for JEditorPane explicitly.
By default, the following types of content are known:
text/plain
Plain text, which is the default the type given isn't recognized. The kit used in this case is
an extension of DefaultEditorKit that produces a wrapped plain text view.
text/html
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download How to use JEditor in Java and more Lecture notes Object Oriented Programming in PDF only on Docsity!

JEDITOR Class (JAVA)

1. OBJECTIVES:

Since this tutorial has been designed for novices to help them learn the fundamental

functioning of JAVA Swing , Jeditor Pane and their structure in Java, the primary goals of

this research effort are as follows:

 To understand what JEditor Pane is?

 To understand the difference between JText Area and JEditor.

 To understand the practical work implementation in JEditor.

 To understand the purpose of using JEditor and its Applications.

 To understand JEditor Coding for programming.

2. CLASS NAME:

The class name for this tutorials discussion is “JEditorpane.”

3. INTRODUCTION:

A JEditorPane is a text component that can handle different text with style. By

default, it can handle plain text, HTML, and Rich Text Format (RTF). A

JEditorPane is primarily used to display an HTML document with only basic

HTML elements.

This class has setContentType() and setText() methods.

 setContentType("text/plain"): This method is used to set the content

type to be plain text.

 setText(text): This method is used to set the initial text content.

However, we can build your own “editor kits” to handle specific content type. We can

use setContentType() method to choose the document we want to display and setEditorKit()

method to set custom editor for JEditorPane explicitly.

By default, the following types of content are known:

text/plain

Plain text, which is the default the type given isn't recognized. The kit used in this case is

an extension of DefaultEditorKit that produces a wrapped plain text view.

text/html

HTML text. The kit used in this case is the

class javax.swing.text.html.HTMLEditorKit which provides HTML 3.2 support.

text/rtf

RTF text. The kit used in this case is the class javax.swing.text.rtf.RTFEditorKit which

provides a limited support of the Rich Text Format.

4. CLASS HIERARCHY: Constructor Description

JEditorPane() It creates a new JEditorPane.

JEditorPane(String url) It creates a JEditorPane based on a string containing a URL

specification.

JEditorPane(String type,

String text)

It creates a JEditorPane that has been initialized to the given

text.

JEditorPane(URL initialPage) It creates a JEditorPane based on a specified URL for input.

**5. CONSTRUCTORS:

  1. Functions/Methods:**

the URL being displayed). String getUIClassID()^ Gets^ the^ class^ ID^ for the UI.

protectedInputStrea

m

getStream(URL page) Fetches^ a^ stream^ for

the given URL, which is about to be loaded by the setPage method. static void registerEditorKitForContentType(String^ typ

e,

String classname)

Establishes the default bindings of type to classname. void replaceSelection(String^ content)^ Replaces the currently selected content with new content represented by the given string. Dimension getPreferredSize()^ Returns the preferred size for the JEditorPane. String getContentType()^ Gets^ the^ type^ of content that this editor is currently set to deal with. void read(InputStream^ in,^ Object^ desc)^ This method initializes from a stream. Modifier and Type Field Description

static String HONOR_DISPLAY_PROPER

TIES

Key for a client property used to indicate

whether the default font and foreground color

from the component are used if a font or

foreground color is not specified in the styled

text.

static String W3C_LENGTH_UNITS^ Key^ for^ a^ client^ property^ used^ to^ indicate

whether w3c compliant length units are used for

html rendering.

**7. Fields:

  1. Nested Classes:** Modifier and Type Class Description

protected class JEditorPane.AccessibleJEditorP This class implements accessibility

ane support for the JEditorPane class.

protected class JEditorPane.AccessibleJEditorP

aneHTML

This class provides support for

AccessibleHypertext, and is used in

instances where the EditorKit installed

in this JEditorPane is an instance of

HTMLEditorKit.

protected class JEditorPane.JEditorPaneAccessi

bleHypertextSupport

What's returned by

AccessibleJEditorPaneHTML.getAccess

ibleText

9. Examples: Example1:Write a program which can handle “Hyper links” click events. (HTML JEDITOR) Program

import java.awt.BorderLayout;

import java.awt.Container;

import java.beans.PropertyChangeEvent;

import java.io.IOException;

import java.net.URL;

/w w w .j a v a 2s .com/

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);

} 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( "http://www.java2s.com" );

Output

Example2:Write a program which can generate simple text editor panel and opens the text editor with input text from user using JEditor Pane. (Simple JEDITOR Text Editor) Program

import javax.swing.JEditorPane;

import javax.swing.JFrame;

public class JEditorPaneExample {

JFrame myFrame = null ;

public static void main(String[] a) {

( new JEditorPaneExample()).test();

private void test() {

myFrame = new JFrame("JEditorPane Test");

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myFrame.setSize( 400 , 200 );

JEditorPane myPane = new JEditorPane();

myPane.setContentType("text/plain");

myPane.setText("Sleeping is necessary for a healthy body."

+ " But sleeping in unnecessary times may spoil our health, wealth and st

udies."

+ " Doctors advise that the sleeping at improper timings may lead for obe

sity during the students days.");

myFrame.setContentPane(myPane);

myFrame.setVisible( true );