Solved

Special characters in a TextArea in Java Netbeans

Posted on 2014-03-16
9
840 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Please attach an example file that's problematic
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
Comment Utility
Try this:

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

Expert Comment

by:krakatoa
Comment Utility
IF you want support for the Euro currency symbol as well, then use ISO-8859-15
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
@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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

15 Experts available now in Live!

Get 1:1 Help Now