0

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!

Community
  • 1
  • 1
neurothew
  • 185
  • 1
  • 2
  • 11
  • *"It said that the interface contains a set of methods with empty bodies"* is not entirely true, a `interface` has no methods, it simply describes the what an implementation on the `interface` guarantees to provide – MadProgrammer Aug 05 '15 at 04:31

1 Answers1

1

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you very much! That made my concept clear a lot. – neurothew Aug 05 '15 at 04:44
  • You mentioned that the getStyleDocument returns an object that implements StyledDocument, does it mean that the creation of JTextPane would also create that anonymous object? But it won't be revealed until we use the getStyleDocument function? – neurothew Aug 05 '15 at 04:46
  • The default constructor uses an instance of `EditorKit `(specifically `StyledEditorKit`) which acts as a factory to generate the `StyledDocument`. When dealing with interfaces like this, you should never assume anything about the implementation – MadProgrammer Aug 05 '15 at 04:52