Results 1 to 12 of 12
  1. #1
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Special characters JavaDB into Java

    I have a Derby database with a table that contains string data with special characters. For example, H2O, where the 2 is subscripted. I want to display this string correctly in Java. I'm trying to do this with HTML. But when I import the string from the DB, the HTML formatting is ignored. For example, the database string "<html>H<sub>2</sub>O</html>" does not produce the correct formatting when displayed; that is to say the string is literally displayed as written, not H2O but <html>H<sub>2</sub>O</html>.

    How do I get Java to correctly interpret the HTML that is embedded in the strings in the database tables?

    Or is there a better way to do this?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    It might help to know what you are doing, currently, to "display" the String.

  3. #3
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default display of DB string

    I load a String array from the database (as there are a number of records I need to display), then I load a JLabel with the .setText() method from said array.

    If I simply load the HTML string directly into the array, then set the JLabel, everything is hunky dory.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Ökay? And what, then, is the problem?

  5. #5
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default problem restated

    In essence, I want to display strings that contain special characters (e.g., subscripts) from a DB.

    Here is an example of a string I want to display: "<html>H<sub>2</sub>O</html>"

    Using the following construct

    while (questionsRS.next()) { questionsArray[i] = questionsRS.getString(1); }

    followed by

    questionLbl.setText(questionsArray[i]);

    displays the string literally, i.e., "<html>H<sub>2</sub>O</html>" and not as "H2O" (where the "2" is subscripted).

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Java Code:
            String x = "<html>H<sub>2</sub>O</html>";
            JLabel label  = new JLabel();
            aPanel.add(label);
            label.setText(x);
    This works. And is no different to the setText(yourArray[i]) one you do above.
    Have you checked whether there's any spurious characters in your string?
    Print them out when you get them from the db, ensuring you prefix and suffix some markers so you can see if there's any whitespace hanging around.

  7. #7
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Reply to Tolls

    I did as you suggested. There appears to be nothing else but the intended string <html> ... </html>, and it still doesn't work.

    I'm really stumped. If I load the string into a string array directly (i.e., without accessing the DB), then set the JLabel, it works just fine.

    Something seems to be happening in going from the DB to string array.

    What the problem is, I have no idea.

    Frustrating as hell!

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    There has to e something different about the two Strings.
    If you have a String with the same text in it then it does not matter where it comes from.
    So my code above will work whether that String is hardcoded (as above) or from a DB or from XML or whatever.

    Something in the String from the db is different...or you are doing something different with that String.

  9. #9
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Further Reply to Tolls

    I totally agree. Something is in that string; I just cannot seem to see what it is.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    If you want anyone here to try and figure it out then you'll have to post code and/or cut and paste some solid logs.

  11. #11
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default problem solved

    I have discovered the origin of my problem. In all my debugging I forgot that I am trying to use the setText method not with a Label but with a TextArea! (I gather the TextArea does not support HTML.) I need to use a JTextPane instead.

    Thanks Toll for helping me to figure this out!

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    There had to be something different.

    Top tip...when comparing two techniques and you notice a difference in the results then make sure they are actually comparable in the first place.
    :)

Similar Threads

  1. Special Characters on GUI
    By keo in forum AWT / Swing
    Replies: 4
    Last Post: 05-01-2011, 10:40 PM
  2. Special characters question
    By alexalex1234 in forum Advanced Java
    Replies: 1
    Last Post: 03-24-2011, 11:33 AM
  3. XML with special characters
    By Kaizah in forum XML
    Replies: 1
    Last Post: 11-06-2009, 02:26 PM
  4. Replies: 1
    Last Post: 03-27-2009, 10:18 AM
  5. special characters
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •