?
Solved

Special characters in a TextArea in Java Netbeans

Posted on 2014-03-16
9
Medium Priority
?
974 Views
Last Modified: 2014-03-16
Hi experts

I'm in the process of developing a program in Java NetBeans which uses a TextArea, and as I am from Denmark I would like to make it capable of showing some special Danish characters.
Therefore, I would be very pleased if someone can tell me how to convince a TextArea to use special characters, in this case some special Danish characters

Best regards
JorgenAx
0
Comment
Question by:JorgenAx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 39932393
Could you tell us how you are trying to get those 'special' characters into the text area, ie. are you typing them in or are you loading them from somewhere? If you are loading them from somewhere, can you please post the code that you are using to do this.
0
 

Author Comment

by:JorgenAx
ID: 39932397
I am loading from a file, using the following code, which places the textarea in a JFrame:

public TextFrame() {
        initComponents();
        try{
                FileReader reader = new FileReader(TextFileName);
                BufferedReader br = new BufferedReader(reader);
                jTextArea1.read(br, null);
                br.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39932410
Ok, so I assume that the encoding used in the file that you are loading is different to the default encoding used by the JVM, which is what FileReader will use.

If this is the case, you have two options... If the file that you are loading can vary widely, and therefore the encoding may vary, then leave it up to the user/you to set the JVM file encoding when starting the app by specifying   -Dfile.encoding=UTF-8    on the java command line.

Alternatively,  if the file will often be the same or you can at least constrain the types of file encodings to support, you can explicitly set the encoding in your code. Or you may ask the user for the encoding and use that in your code. Either way, you set the encoding like this...
public TextFrame() {
        initComponents();
        try{
                Reader reader = new InputStreamReader(new FileInputStream(TextFileName), "UTF-8");
                BufferedReader br = new BufferedReader(reader);
                jTextArea1.read(br, null);
                br.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:JorgenAx
ID: 39932432
How do I add command line options to a Java Swing Application?

The alternative did not work. It is still unable to read Danish letters.

best regards
JorgenAx
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39932444
Please attach an example file that's problematic
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 2000 total points
ID: 39932460
Try this:

new InputStreamReader(new FileInputStream(new File("myfile","ISO-8859-1")));
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39932472
IF you want support for the Euro currency symbol as well, then use ISO-8859-15
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39933269
@JorgenAx,

You didn't feel the need to assign points to more than one answer?

Considering that I did say to use the encoding that the file used (the UTF-8 was just for an example), and also considering that the accepted answer is not 100% correct and you would have had to use code more like what I posted, just with the different encoding scheme specified.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question