Solved

Special characters in a TextArea in Java Netbeans

Posted on 2014-03-16
9
863 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
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 35

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 35

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 500 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 35

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

777 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