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!