Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Get foreign characters to appear in a JTextArea or other Swing component?  (Read 1786 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Posted 2010-06-09 17:46:47 »

I've got some Swing components (a JTextArea and a JList) that have a bunch of strings in them. The strings are often foreign, as in I'll have accented letters like é ï, etc.

Does anyone know a way I can get Swing to not do retarded character corruption like "Jouer √† √™tre Dieu" instead of "Jouer être Dieu." If I write the data to a file there is no corruption, so I know it's got something to do with Swing. I've tried changing the Font and that doesn't seem to help.

Also, this isn't as important, but it would be nice for ' to be translated to ' in the display. However, I don't want to have to deal with putting in HTML to format it correctly, so putting it into a HTML parsing view is not a good solution.

See my work:
OTC Software
Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« Reply #1 - Posted 2010-06-09 20:26:24 »

I've never come across any problems with displaying UTF8 Strings in Swing components.
My guess is that the Strings are becoming corrupt before they are reaching Swing - likely because the Strings are being constructed from data incorrectly assumed to be 1 byte/character.

Where are the Strings coming from? what format are they being provided in? and how are you constructing the String objects?

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #2 - Posted 2010-06-09 21:21:28 »

I've never come across any problems with displaying UTF8 Strings in Swing components.
My guess is that the Strings are becoming corrupt before they are reaching Swing - likely because the Strings are being constructed from data incorrectly assumed to be 1 byte/character.

Where are the Strings coming from? what format are they being provided in? and how are you constructing the String objects?
The String objects are fine when I spit them out (later) to a text file.

I read them from a file with BufferedReader, store them in a String object, then put them into a StringBuffer, and JTextArea.setText(buffer.toString()). Then later a write them to a file, which has no formatting problems.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 818
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2010-06-09 22:01:08 »

I read them from a file with BufferedReader

So what charset do you specify? (if any)


You need:
new BufferedReader(InputStreamReader(new FileInputStream(...), "UTF-8") )

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2010-06-09 22:25:19 »

So what charset do you specify? (if any)


You need:
new BufferedReader(InputStreamReader(new FileInputStream(...), "UTF-8") )
Yeah, that did the trick. Thanks, Riven. Weird that it was able to output the text again just fine later with a PrintWriter.

See my work:
OTC Software
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 818
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-06-09 22:26:52 »

Yeah, that did the trick. Thanks, Riven. Weird that it was able to output the text again just fine later with a PrintWriter.

That's called: two bugs that cancel eachother out.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #6 - Posted 2010-06-09 22:33:31 »

Also, this isn't as important, but it would be nice for ' to be translated to ' in the display. However, I don't want to have to deal with putting in HTML to format it correctly, so putting it into a HTML parsing view is not a good solution.

Assuming you don't want to just do a replaceAll("'", "'") in your text, you could accomplish this with a custom ListCellRenderer.  Just a DefaultListCellRenderer subclass that does this, assuming your list items are Strings:

1  
2  
3  
4  
5  
6  
7  
8  
public Component getListCellRendererComponent(JList list,
                                              Object value,
                                              int index,
                                              boolean isSelected,
                                              boolean cellHasFocus) {
   value = ((String)value).replaceAll("'", "'");
   return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2010-06-09 23:22:55 »

That's called: two bugs that cancel eachother out.
Smiley

Thanks BoBear, that'll do the trick. I didn't want to replace any part of the string because I need the string to stay as-is, I just want to display it differently.

See my work:
OTC Software
Offline ddyer
« Reply #8 - Posted 2010-06-09 23:25:22 »

You will also find that you have to use "heavyweight" mode in menus if your menus include full unicode.

JPopupMenu.setDefaultLightWeightPopupEnabled(false);
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 818
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2010-06-09 23:26:34 »

Smiley

Thanks BoBear, that'll do the trick. I didn't want to replace any part of the string because I need the string to stay as-is, I just want to display it differently.
Keep in mind that String.replaceAll() uses regex... so don't just pass in your exact-match-Strings.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (33 views)
2014-10-17 03:59:02

Norakomi (25 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (74 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!