Jeditorpane and jtextpane

Source: Internet
Author: User
1: Use the jeditorpane component:
The class hierarchy of jeditorpane:
Java. Lang. Object
-- Java. AWT. Component
-- Java. AWT. Container
-- Javax. Swing. jcomponent
-- Javax. Swing. Text. jtextcomponent
-- Javax. Swing. jeditorpane
Jeditorpane inherits the jtextcomponent class, so it can also use methods in the jtextcomponent abstract class. The main function of jeditorpane is
Different file formats are available. Jeditorpane supports three file types: the first is plain text, and its type is represented as "text/plain ".
Is the most commonly used TXT file. This type of file can be edited using notepad, WordPad, and other document editing software. The second type is the RTF type, and its representation
For "text/rtf", this type of file features special effects such as font scaling, deformation, and coloring of text content. The third type is HTML, that is, on the network.
The web page type browsed in the format of "text/html". The characteristics of such files are clear to everyone. In addition to the font effect
Add images, hyperlinks, and other related functions. However, jeditorpane is not a full-featured web browser. It only supports simple HTML syntax. jeditorpane
HTML files are mainly used to create online auxiliary instruction files.
Jeditorpane constructor:
Jeditorpane (): Creates a new jeditorpane.
Jeditorpane (string URL): Creates a jeditorpane based on the detailed URL string.
Jeditorpane (string type, string text): Creates a specified string text and specifies the type of jeditorpane to be initialized.
Jeditorpane (URL initialpage): uses a detailed URL string as the input value to create a jeditorpane.

1-1: Construct the jeditorpane component:
We put an HTML file in the constructed jeditpane:
Import javax. Swing .*;
Import javax. Swing. event .*;
Import java. Io .*;
Import java. AWT. event .*;

Public class jeditorpane1 {
Public static void main (string [] ARGs ){
Jeditorpane editpane = NULL;
Try {
File file = new file ("docs/jeditorpane_1.html ");
String STR = file. getabsolutepath (); // obtain the absolute path of the file location
STR = "file:" + STR; // combine the absolute path into a complete input string

/* Use the setpage () method to load the file indicated by the path in the string to jeditorpane.
* In the setpage () method, the input data is a string type string. In fact, this construction method is equivalent to using another jeditorpane constructor.
* Jeditorpane (string Str. Therefore, if we rewrite the following two lines of programs:
* Editpane = new jeditorpane (STR );
* The results will be the same, so we will not describe this constructor.
*/
Editpane = new jeditorpane (); // construct an empty jeditorpane
Editpane. setpage (STR );
} Catch (ioexception IOE ){
IOE. printstacktrace (system. Err );
System. Exit (0 );
}
/* Use the seteditable () method to set jeditorpane to uneditable. Please note that this line is very important. If we set this method to true, we will
* The features of HTML files, such as hyperlink functions, are lost. Therefore, when using the following jeditorpane2 example
* Remove (set false). This hyperlink function does not work currently. This section will be introduced in jeditorpane event processing.
*/
Editpane. seteditable (false );
Jframe F = new jframe ("jeditorpane1 ");
F. setcontentpane (New jscrollpane (editpane ));
F. setsize (200,200 );
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
}
}
We mentioned earlier that jeditorpane supports three types of file formats. In the above example, we did not see the steps to set the file format, because
The system automatically identifies the file type according to the input file name. If you want to set the file type, you can use the setcontenttype () method or
Set it directly in the jeditorpane constructor. For example:
Import javax. Swing .*;
Import javax. Swing. event .*;
Import java. AWT. event .*;

Public class jeditorpane2 {
Public static void main (string [] ARGs ){
String STR = new string ("this is a test. \ nthis is line2! \ Nthis is line 3! ");
Jeditorpane editpane = new jeditorpane ("text/plain", STR );
Editpane. seteditable (false );

Jframe F = new jframe ("jeditorpane2 ");
F. setcontentpane (New jscrollpane (editpane ));
F. Pack ();
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
}
}
The URL class is constructed as a jeditpane parameter, but note that in this method, the computer must connect to the local network or the Internet.
The program will not be able to find the location specified by the URL and cause an exception to make the program unable to operate. Let's take a look at the following example!
Import javax. Swing .*;
Import javax. Swing. event .*;
Import java. AWT. event .*;
Import java.net .*;
Import java. Io .*;

Public class jeditorpane3 {
Public static void main (string [] ARGs ){
Jeditorpane editpane = NULL;
Try {
URL address = new URL ("http://www.sina.com.cn ");
Editpane = new jeditorpane (Address );
} Catch (malformedurlexception e ){
System. Out. println ("malformed URL:" + E );
} Catch (ioexception e ){
System. Out. println ("ioexception:" + E );
}
Editpane. seteditable (false );
Jframe F = new jframe ("jeditorpane3 ");
F. setcontentpane (New jscrollpane (editpane ));
F. setsize (200,250 );
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
}
}

1-2: jeditorpane event handling:
In jeditorpane files, HTML files are the most commonly used part of event processing, because HTML files themselves have the hyperlink function to serve as article links.
. Do you still remember the first example in this section? Didn't we load an HTML file into jeditorpane? Although there are indeed hyperlinks and
The image information is displayed, but have you found that you do not respond when you want to click the hyperlink? That's because we didn't add anything to jeditorpane.
Processing Mechanism. We rewrite jeditorpane1.java to add the event processing mechanism so that jeditorpane has a normal hyperlink function. Example:
Import javax. Swing .*;
Import javax. Swing. event .*;
Import java. Io .*;
Import java. AWT. event .*;

Public class jeditorpane4 {

Public static void main (string [] ARGs ){

Jeditorpane editpane = NULL;
Try {
File thef = new file ("docs/jeditorpane_1.html ");
String STR = thef. getabsolutepath ();
STR = "file:" + STR;
Editpane = new jeditorpane ();
Editpane. setpage (STR );
}
Catch (ioexception IOE ){
IOE. printstacktrace (system. Err );
System. Exit (0 );
}
Editpane. seteditable (false );

Final jeditorpane thepane = editpane;
// Use the inner class method to compile the corresponding operation class for triggering a hyperlink event
Editpane. addhyperlinklistener (New hyperlinklistener (){
Public void hyperlinkupdate (hyperlinkevent HLE) {// overwrite the hyperlinkupdate () method. When a hyperlink event is triggered
// Run in different segments.
Try {
// Determine whether the operation is a hyperlink operation. If the operation is true, place the new HTML file in jeditorpane,
// The operation is (thepane. setpage (HLE. geturl ());)
If (HLE. geteventtype () = hyperlinkevent. eventtype. Activated)

}
Catch (ioexception IOE ){
IOE. printstacktrace (system. Err );
}
}
});

Jframe F = new jframe ("jeditorpane4 ");
F. setcontentpane (New jscrollpane (editpane ));
F. setsize (200,250 );
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
} // End of main ()
} // End of class jeditpane1
What should we do if we need event processing mode in plain text or RTF text? You still remember
In jtextarea, how does one add the event processing mode? That's right! This is also the same practice in jeditorpane, that is, the use of documentlistener Interface
Mechanism to handle, because the practice is quite similar, so we will not repeat the description here.

2: Use the jtextpane component:
The class hierarchy of jtextpane:
Java. Lang. Object
-- Java. AWT. Component
-- Java. AWT. Container
-- Javax. Swing. jcomponent
-- Javax. Swing. Text. jtextcomponent
-- Javax. Swing. jeditorpane
-- Javax. Swing. jtextpane
We have previously introduced the jtextarea class. Although jtextarea can meet our needs in some functions, when we want to add more changes
(Such as adding color to text, inserting images...) you will find that the jtextarea class cannot do this at all. To implement these functions, we must use the subclass of jeditorpane:
Jtextpane. Jtextpane provides many text processing functions, such as changing colors, font scaling, text style, and adding images. Let's take a look at the structure of jtextpane.
Creation method:
Jtextpane constructor:
Jtextpane (): Creates a new jtextpane.
Jtextpane (styleddocument DOC): Creates a new jtextpane in the specified file mode.
2-1: jtextpane features:
I believe that everyone has used word to write reports or articles, so you will surely know that we can make many changes to the text in the articles in Word. These changes are
Is the "attribute" change of the text. Because the effects produced in jtextpane are almost all caused by changes in attributes, the Class components that change attributes are less in jtextpane.
No. Therefore, before introducing how to construct jtextpane, we should first introduce two attribute classes that are often used in jtextpane:
Simpleattributeset and styleconstant.
Attribute changes are originally handled using attributeset interface, but this interface contains too many methods.
Attributeset interface needs to implement all the methods in this interface, which is not an ideal practice for programming; Java also provides
Simpleattributeset class implements attributeset interface. Therefore, as long as we directly use simpleattributeset class, we can have attributeset
Instead of writing attributeset methods one by one. In addition, the styleconstant class provides many common buteset classes.
Attribute key and method to set or obtain the status of jtextpane content. The styleconstant class contains many common attribute settings, including
And border blank area settings, text font/size/type settings, background color settings, and so on. Using these two classes to help design jtextpane makes jtextpane richer
.
Jtextpane is a component designed for text and layout processing. Jtextpane's design concept for the content of the input area is similar to the word design concept, that is, in
The text structure in jtextpane has the concept of paragraph. The concept of "paragraph" is to use the [enter] key as the demarcation point of each segment, and each time you press the [enter] key, a segment is added.
. Do you remember the element storage mode we mentioned in jtextarea? Jtextpane uses the same method, but the difference is that the storage planning method is different.
In jtextarea, there are no paragraphs, but the [enter] key is used as the demarcation between the two elements. In jtextpane, the root section is the entire editing area.
Point. Each section is a branch node, and each character is a leaf node to store files. Because jtextpane stores data in this way, jtextpane can also
Different paragraphs are set to different attributes like Word files. For example, the first section is italic, the font size is 14 characters, the second section is italic, And the font color is
Blue, 2 cm to the left border, and so on. In addition, we can also set the distance between the input text and each boundary in the jtextpane editing area. From these features
Jtextpane is a component with many practical functions.

2-2: Construct the jtextpane component:
After learning about the features of jtextpane, let's take a look at what jtextpane can do. In the following example, we will
Set the color, bold italic, and bottom line of the text in the area.

Import javax. Swing .*;
Import javax. Swing. Text .*;
Import java. AWT. event .*;
Import java. AWT .*;

Public class jtextpane1 {
Private jtextpane textpane;

Public jtextpane1 (){
Textpane = new jtextpane ();
Textpane. setbackground (color. Black );
Textpane. seteditable (false );
}
Public void setyellow_bold_20 (string Str ){
Simpleattributeset attrset = new simpleattributeset ();
Styleconstants. setforeground (attrset, color. Yellow );
Styleconstants. setbold (attrset, true );
Insert (STR, attrset );
}
Public void setblue_italic_bold_22 (string Str ){
Simpleattributeset attrset = new simpleattributeset ();
Styleconstants. setforeground (attrset, color. Blue );
Styleconstants. setitalic (attrset, true );
Styleconstants. setfontsize (attrset, 24 );
Insert (STR, attrset );
}
Public void setred_underline_italic_24 (string Str ){
Simpleattributeset attrset = new simpleattributeset ();
Styleconstants. setforeground (attrset, color. Red );
Styleconstants. setunderline (attrset, true );
Styleconstants. setitalic (attrset, true );
Styleconstants. setfontsize (attrset, 24 );
Insert (STR, attrset );
}
// The main purpose of this method is to insert a string into jtextpane.
Public void insert (string STR, attributeset attrset ){
Document docs = textpane. getdocument (); // use the getdocument () method to obtain jtextpane's document instance.0
STR = STR + "\ n ";
Try {
Docs. insertstring (Docs. getlength (), STR, attrset );
} Catch (badlocationexception BLE ){
System. Out. println ("badlocationexception:" + BLE );
}
}
Public component getcomponent (){
Return textpane;
}
Public static void main (string [] ARGs ){
Jtextpane1 pane = new jtextpane1 ();
Pane. setyellow_bold_20 ("this is line 1, yellow, bold, size 20 ");
Pane. setblue_italic_bold_22 ("this is line 2, Blue, italic, bold, size 22 ");
Pane. setred_underline_italic_24 ("this is line 3, Red, underline, italic, size 24 ");

Jframe F = new jframe ("jtextpane1 ");
F. getcontentpane (). Add (pane. getcomponent ());
F. setsize (450,180 );
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
}
}

If you want to place images or other components (such as tables or buttons) on jtextpane, you can use inseticon () and insertcomponent () provided by jtextpane respectively ()
Method To achieve this effect.
As for the construction of another jtextpane, the difference is that jtextarea adopts Document Interface while jtextpane adopts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.