简体   繁体   中英

Questions about relationship between StyledDocument and JTextPane, and the proper usage of interface

I am new to java and working on java swing GUI. Recently I read a post: Centering Text in a JTextArea or JTextPane - Horizontal Text Alignment

The solution inside worked perfectly but I have some conceptual questions to ask.

I read the introduction of interface and classes in the oracle website. It said that the interface contains a set of methods with empty bodies, then the class which implement such interface would need to declare all the methods mentioned in the interface in order to be successfully complied.

Here comes my question: After I read through the documents, I knew that StyledDocument is an interface, but what does the following code means?

StyledDocument doc = textPane.getStyledDocument();

My interpretation is that, I guess that a JTextPane implements the StyledDocument internally so that this line of code is to receive the existing StyledDocument ( But it should not be called an instance as we could not create instance of interface, How should I describe it?) . If this is true, then JTextPane should have all methods defined in the StyledDocument interface.

Am I correct in the above paragraph?

Then, I tried not to write this two lines of code:

StyledDocument doc = textPane.getStyledDocument();
doc.setParagraphAttributes(0, doc.getLength(), center, false);

But I directly used:

textPane.setParagraphAttributes(center, false);

And this also worked perfectly.

So, are there any differences between the two implementations?

Is my code a good practice to do so?

Thank you very much for help!

I think you're getting stuck on the concept of polymorphism, have a look at the trail on Polymorphism for starters.

My interpretation is that, I guess that a JTextPane implements the StyledDocument internally so that this line of code is to receive the existing StyledDocument (But it should not be called an instance as we could not create instance of interface, How should I describe it?). If this is true, then JTextPane should have all methods defined in the StyledDocument interface.

No. The getStyledDocument method returns an object which implements the StyledDocument interface. JTextPane does not implement this functionality directly, but delegates the requirements to the instance of the object which implements the StyledDocument interface.

Together they provide the means by which styled text can be displayed. This is a concept of the Model-View-Controller paradigm, where non-visual functionality (the model or StyledDocument ) is separated from the view (the JTextPane )

Then, I tried not to write this two lines of code:

 StyledDocument doc = textPane.getStyledDocument(); doc.setParagraphAttributes(0, doc.getLength(), center, false); 

But I directly used:

 textPane.setParagraphAttributes(center, false); 

And this also worked perfectly.

So, are there any differences between the two implementations?

Yes and no. setParagraphAttributes delegates the functionality to the StyledDocument , as the below snippet of code, taken from JTextPane demonstrates:

public void setParagraphAttributes(AttributeSet attr, boolean replace) {
    int p0 = getSelectionStart();
    int p1 = getSelectionEnd();
    StyledDocument doc = getStyledDocument();
    doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}

It simply acts as a convenience method, there to make your life a little simpler

Is my code a good practice to do so?

I see no issue with using the functionality provided to achieve your goals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM