Solved

Special characters in a TextArea in Java Netbeans

Posted on 2014-03-16
9
934 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This is about my first experience with programming Arduino.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Progress

688 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