3

I'm studying about the structure of text components in Swing programs.

As far as I understand, JTextComponent is essentially divided into a view and a model. The model is an instance of a class implementing Document, containing all of the text and offering ways to manipulate it, and the View presenting the text visually.

However I don't understand exactly where, how and why an EditorKit is used. I'm not sure if it encapsulates ('owns') the model (Document), or if the Document encapsulates it. And not sure where the view fits inside all of this.

So two questions:

1- Please describe the relations between the view, Document and EditorKit in JTextComponents. What encapsulates what, what interacts with what, and why?

2- Please explain the functionality and role of EditorKit.

Thank you for your help

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131

3 Answers3

2

EditorKit is the abstract parent of DefaultEditorKit and StyledEditorKit, both of which export useful Action classes that operate on the Document model common to text components. In this example, editor kit actions update the Document, which indirectly updates the listening view component. Charles Bell's HTMLDocumentEditor, cited here, is a related example.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for answering. In a classic MVC implementation, actions on the UI (which is the view) are reported to the controller, which then accordingly invokes actions on the model. Finally the model notifies the view (usually via Observer pattern) that the data has changed, and the view updates itself accordingly. I'm interested in knowing if this is the case with the internal structure of JTextComponents. Is it the case that when the user does something (e.g. types text), the view invokes something on the EditorKit, which then invokes something on the Document? Is this how it works? – Aviv Cohn May 02 '14 at 11:09
  • If not, could you give me a practical high-level example of what happens internally in a JTextComponent when the user types in text? – Aviv Cohn May 02 '14 at 11:16
  • Someone just told me that the UI and EditorKit are both the controller. Still, could you give me a high-level example of how it works internally when the user types text in the component? – Aviv Cohn May 02 '14 at 11:27
  • Right; `EditorKit` actions alter the `Document` (model), which notifies any listening text component (view). In Swing, I think of the user as the controller; more [here](http://stackoverflow.com/a/3072979/230513). – trashgod May 02 '14 at 11:58
  • When the program calls `setText()` on a `JTextArea`, what is invoked internally? A method on the `EditorKit`? – Aviv Cohn May 02 '14 at 12:24
  • To follow the execution path, I'd set a breakpoint in the debugger, as suggested [here](http://stackoverflow.com/a/12770976/230513). – trashgod May 02 '14 at 19:20
2

EditorKit is controller like thing which allows working with specified content type. It has a reference to the Document (model) and listen changes.

EditorKit provides Reader and Writer to load/store the specific type of Document (e.g HTMLDocument or RtfDocument). Also it provides ViewFactory to create views for different Document's Elements (nodes and leaves).

Also EditorKit provides list of actions possible for the type of content.

You can try this to see how an EditorKit could be created and used.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • If the EditorKit is a kind of controller for the internal system of JTextComponents, than why doesn't it also have a reference to the View? Could you try to explain what happens internally when a user types something in a JTextArea? Does the View invoke the EditorKit, which then manipulates the Document? (Like a classic MVC?) Or is it something else? – Aviv Cohn May 02 '14 at 10:31
  • In fact it has indirect reference to View. editorKitInstance.getUI().getRootView(JTextComponent tc). When EditorKit is sety it adds listeners to the Document. When document is changed (when user types) it reflects the changes and updates Views – StanislavL May 02 '14 at 10:38
  • In a classic MVC, the view is the UI so it is the one that receives user actions. When it receives a user action (e.g. a button click), it reports it to the controller. The controller than manipulates the model accordingly, and finally the model notifies the view (via Observer pattern) about the changes and the view updates itself. My question is this: is this also exactly how things work internally with a JTextComponent? – Aviv Cohn May 02 '14 at 10:41
  • It's not ideal world. EditorKit+UI is the controller. Kit also changes model e.g. by loading new content or applying some specific actions. – StanislavL May 02 '14 at 11:12
  • I see. So when the user makes some action on the component (e.g. types text), the EditorKit (which is part of the UI) decides how to affect the model (Document). Is this accurate? – Aviv Cohn May 02 '14 at 11:20
  • It's very common. UI listens changes and actions do the same. They chnage Document and the chnages are reflected in views. – StanislavL May 02 '14 at 11:27
0

From http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html "A controller, known as an editor kit, that reads and writes text and implements editing capabilities with actions."

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Thanks for answering. I read this page in the docs. Let me see if I understand what the Editor Kit is based on what you quoted: when the user makes an action on the UI, the Editor Kit is the object that takes care of translating it to actions on the model. Like a classic MVC implementation. Is this correct? Can you tell me if I'm missing something, or if this is indeed the job of the Editor Kit in the structure of Swing components? – Aviv Cohn May 01 '14 at 21:30
  • It's not all Swing components, just `JTextComponent` and its subclasses. But yeah, it's a switchable controller that allows you to have a basic class (such as `JTextPane`) and customize its behaviour and appearance based on the implementation used. – Kayaman May 02 '14 at 08:56