CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Wei Cui
Level 39
Newark
  • 09.04.2020
  • 638views
  • 1comment

how to meet last request

Question about the task HTML Editor (part 16)
Java Multithreading,  Level 8,  Lesson 15
Resolved

Add a setPlainText(String text) method to the controller. It will write the passed text with HTML tags to document. Implement it as follows:
16.1. Reset the document.
16.2. Create a new StringReader based on the passed text.
16.3. Call the read() method in the HTMLEditorKit class. It reads data from the reader into the document field.
16.4. Be sure that the method does not throw exceptions. They just need to be logged.

Requirements:
  • The Controller class must have a public setPlainText(String text) method that writes the text with HTML tags to the document.
  • The setPlainText(String text) method should reset the document using the resetDocument() method.
  • The setPlainText(String text) method should create a new StringReader based on the parameter.
  • The setPlainText(String text) method should call the read() method on the HTMLEditorKit object.
  • If an exception occurs in the setPlainText(String text) method, the exception must be logged using the ExceptionHandler class's log method.
package com.codegym.task.task32.task3209; import javax.swing.text.BadLocationException; import javax.swing.text.html.HTMLDocument; import javax.swing.text.html.HTMLEditorKit; import java.io.File; import java.io.IOException; import java.io.StringReader; public class Controller { private View view; private HTMLDocument document; private File currentFile; public Controller(View view) { this.view = view; } public void resetDocument() { if (document != null) { document.removeUndoableEditListener(view.getUndoListener()); } HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); document = (HTMLDocument) htmlEditorKit.createDefaultDocument(); document.addUndoableEditListener(view.getUndoListener()); view.update(); } public void init() {} public void exit() { System.exit(0); } public HTMLDocument getDocument() { return document; } public void setPlainText(String text) { resetDocument(); StringReader stringReader = new StringReader(text); HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); try { htmlEditorKit.read(stringReader, document, 0); } catch (IOException e) { ExceptionHandler.log(e); } catch (BadLocationException e) { ExceptionHandler.log(e); } } public static void main(String[] args) { View view = new View(); Controller controller = new Controller(view); view.setController(controller); view.init(); controller.init(); } public static class ExceptionHandler { public static void log(Exception e) { } } }
+2
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Satya Vath
Level 31 , Vijayawada, India
14 April 2020, 12:11solution
Why did you create another ExceptionHandler in Controller Class Remove it
+2
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.