Solved

Special characters in a TextArea in Java Netbeans

Posted on 2014-03-16
9
855 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Not needed 13 95
detecting  the added row index in a datagridview 3 61
Java - Why doesn't this JFrame work 3 45
even odd program using while loop 3 29
A short article about a problem I had getting the GPS LocationListener working.
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now