Results 1 to 12 of 12
- 07-01-2011, 08:23 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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?
- 07-01-2011, 08:46 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
It might help to know what you are doing, currently, to "display" the String.
- 07-01-2011, 08:57 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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.
- 07-01-2011, 09:10 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Ökay? And what, then, is the problem?
- 07-01-2011, 09:23 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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).
- 07-04-2011, 11:04 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
This works. And is no different to the setText(yourArray[i]) one you do above.Java Code:String x = "<html>H<sub>2</sub>O</html>"; JLabel label = new JLabel(); aPanel.add(label); label.setText(x);
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.
- 07-04-2011, 04:17 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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!
- 07-04-2011, 04:20 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 07-04-2011, 04:22 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Further Reply to Tolls
I totally agree. Something is in that string; I just cannot seem to see what it is.
- 07-04-2011, 04:57 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 07-04-2011, 05:08 PM #11
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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!
- 07-04-2011, 05:24 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
Special Characters on GUI
By keo in forum AWT / SwingReplies: 4Last Post: 05-01-2011, 10:40 PM -
Special characters question
By alexalex1234 in forum Advanced JavaReplies: 1Last Post: 03-24-2011, 11:33 AM -
XML with special characters
By Kaizah in forum XMLReplies: 1Last Post: 11-06-2009, 02:26 PM -
How can i retain special characters in server side (java) after passing from client
By Malathi in forum Web FrameworksReplies: 1Last Post: 03-27-2009, 10:18 AM -
special characters
By ravian in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks