Java Makes Me Cry



  • I have been working on creating a Java based text editor for some time now.  I wanted it to create HTML pages so that the files could be used incase something went wrong with the software, you wanted to take the files to another computer, or simply wanted to view it in a browser.  Enter the antagonist of the story; JEditorPane.  Sun's site claimed that it can display web pages, which it can; the problem occures when the you try to edit the text.  JEditorPane automatically "fixes" what you enter by puting it in paragraph tags if you use any of the helpful methods such as JEditorPane.replaceSelection(String newText), which replaces the currently hilighted portion of text with newText.  Thanks to the paragraph tags, any tags I insert using this method get ignored.  Even if those tags were gone Java also "fixes" what you enter by replacing the  < > with &gt and &lt so that the tags are interpreted as text.  I could simply get the text from the Pane, change it, and then make it use the new text; except that when I get the carret's position it is relative to the text being displayed, not the relative to the underlining text.  So I thought I would simply tell java to treat the text as plain instead of HTML, make the changes, and then tell it display as HTML again, but good old 'helpful" Java automatically gives you a nice fresh document if you do that.  Running out of ideas I next decide to extend the editor kit that was doing the displaying, override the methods that I thought were adding the tags and leave the rest.  Sadly the Javadoc only talks about DefaultEditorKit, which does not display HTML.  I cannot find what class does handles adding the tags.  Currently I am  doing something that I am not proud of to make it display somewhat correctly, but it is giving me problems.

    I write this for two reasons:
    1. Anyone that has had to deal with the "Advance Swing" components of Java will hopefully get a chuckle.
    2. I NEED HELP!  Now I know this is not exactly the right forum to post this, but I am despret.  I have spent hours pouring over the Java help files, the official Javadoc, and forums.  I have Googled everthing I can think of.  I have even refered to several different books trying to find the answer.  If anyone has any possible solution, or a story of their own GUI horror please share.  I am also willing to do anything involving CSS to page the <p></p> not go to a new line.  This is what I am currently working, but it does not look promising.  I am starting to lose sleep over this.

    Just remember: Java is just like Jaws, it is really big and it bites.



  • Yay, yet another java editor



  • (thread moved to Coding related help&questions)




  • are you making a text editor or a really bad WYSIWYG HTML editor?



  • I am trying to make a text editor that allows the user to specify what quick keys and what buttons (such as a bold button) are displayed.  The goal is to create a text editor that gives the user as much control as they want, but is also designed for speed.  It will be part of a program I am making to help create/organise notes, grades, quizzes, class schedules, and generally help people survive college.

    If you are wondering who I am, and if I am trying to get you to help me make money, please understand I plan on releasing the software as freeware if I get it to work.



  • I wasn't worried that you were trying to make money or anything.  however what your describing is not a text editor.  you're describing a word proccessor.  I ake it your storing everything as plain text in HTML formatting.



  • @Justin Miller said:

    I am trying to make a text editor that allows the user to specify what quick keys and what buttons (such as a bold button) are displayed.  The goal is to create a text editor that gives the user as much control as they want, but is also designed for speed.  It will be part of a program I am making to help create/organise notes, grades, quizzes, class schedules, and generally help people survive college.

    If you are wondering who I am, and if I am trying to get you to help me make money, please understand I plan on releasing the software as freeware if I get it to work.

    That sounds like quite an ambitious project!  Are you creating this as part of a class project, possibly a course in software engineering or java, or is this something you're devising primarily for your own use?



  • Yeah, I did get a small chuckle.  But only a small, ironic chuckle.



    The javax.swing.text package is probably the single hardest package to
    master in all of the Java library.  I'm a lot more comfortable
    with it than I used to be, but even I wouldn't attempt to do what
    you're doing, for the reasons you're now discovering and for many other
    similar reasons.



    JEditorPane is barely adequate as an HTML editor.  Considering
    that even Mozilla's composer isn't good at keeping tags clean, I'd be
    amazed if JEditorPane could do it, especially considering it's had
    few updates or improvements since 1998.



    There is, of course, an open RFE in the Java Bug Database asking for
    decent HTML 4.01 support and XHTML support.  It has so many votes
    it's in the top 25: 




    I mention that because when that improvement finally comes along, it
    will probably force vast improvements to the handling of the DOM as
    well, which will probably solve your problems.



    There is nothing you can do to get a better HTML editor, though I've
    occasionally wondered how difficult it would be to port the Gecko
    engine (Mozilla's HTML rendering code) to Java.



    If you're just trying to make a good editor, I suggest you avoid using
    HTML.  Use a JTextPane (it's a subclass of JEditorPane), and use a
    DefaultStyledDocument.  I have done this with reasonable
    success.  (I wrote an ANSI terminal emulator, complete with
    colors, underline, blinking, and so on.  I know that's not
    impressive, but at least I got comfortable with the APIs that affect
    StyledDocuments.)



  • Initually it will be for my own use, as well as for anyone that wishes to use it, until I get it working well enought that I think it is truely worth using.  The end goal is to make a piece of software that will help college students, especially those with learning disabilites, excel.



  • The note section of the program will hopefully be in HTML/plain text, but there will also be portions that will be XML or comma delimated files.



  • Thank you VGA, I will look into using a JTextPane and DefaultStyledDocument.  I wanted to use HTML so that the documents could still be used in case something went wrong with the program ( you don't want your class notes to become unusable), and because CSS allow you to set up a personal look and feel that would best fit the student, but at this point I am willing to try anything.

    I am also glad to know that this is an actual problem, and not just me misunderstanding the documentation.



  • I've had to use the JText/Editor stuff before, and it wasn't pretty. This was for a 3rd semester software engineering project in a group of 2x4 students, about 15,000 lines of code.

    All we needed it for was to display pre-generated HTML in the help system, and for notes/explanations within the program itself. The documentation for the classess is very poor, and a lot of the work that it does is hidden in the internals, and not easily adaptable to any specific requirements. It's just a big pain in the ass to make it do what you want.
    If it doesn't already do exactly what you need, then i wouldn't bother with it, because trying to modify it will probably be more work than writing something that's perfect for your situation.

    I don't know if the control even does CSS, or enough of CSS to make it useable. I would recommend looking elsewhere for a solution that will work better / be less effort.

    1) Fall back to wiki-like documents that are then marked up for displaying. Works well, but isn't very user friendly. Of course, people familiar with wikis may love it, but in general I think this is a bit of a pain.
    2) Look for an external editor that you can use. Word processors in Java must've been done before.
    3) Embed an external program. A workmate showed me a small program that embedded OpenOffice inside a java program (well, an Eclipse-based product.) This looks awesome, and the editing is as good as it's going to get. You don't have to worry about the editing part of the Project (which you shouldn't, as it's not your focus) and can let OO handle that, and then just manage the resulting ODT files.
    The downside to that is, of course, the increase in program size and complexity. Maybe, instead of OpenOffice you can find a leaner editing component (even WordPad+rtf on windows?).

    Good luck with the project, it will be a great learning experience in any case :)


Log in to reply