Question

I created a file .txt which contains a sentence.

I want to set a Jlabel with this sentence and a picture .gif

So I did this :

(I declared 2 Strings with the path to my .txt and the path to my picture)

Then I asked to set it:

{
        ZoneAlerte1 = new JLabel();
        getContentPane().add(ZoneAlerte1);
        ZoneAlerte1.setText(new UTIL_LireFichierTXT().getText(MessageTest)); 
        ZoneAlerte1.setFont(new java.awt.Font("Tahoma",0,12));
        ZoneAlerte1.setIcon(new ImageIcon(Icon));   
        ZoneAlerte1.setBounds(400, 200, 300, 40);
        ZoneAlerte1.setVisible(true);


}

The thing is: when I run my programm, the Label contains the picture and my text but there's a little square between them, like if there were some unknown character in my sentence.

So I tried to replace this line:

ZoneAlerte1.setText(new UTIL_LireFichierTXT().getText(MessageTest)); by ZoneAlerte1.setText("hello");

And it works perfectly, without the square

Then I guess the problem comes from this method called to read a .txt :

public class UTIL_LireFichierTXT {
    public String chaine="";
    public String fichier ="";
    public static void main(String[] args){

    }


public String getText(String path){
            fichier=path;

        //lecture du fichier texte  
        try
        {
            InputStream ips=new FileInputStream(fichier); 
            InputStreamReader ipsr=new InputStreamReader(ips);
            BufferedReader br=new BufferedReader(ipsr);
            String ligne;
            while ((ligne=br.readLine())!=null)
            {
                //System.out.println(ligne);
                chaine+=ligne+"\n";
            }
            br.close(); 
            //System.out.println(chaine);
            return chaine;
        }       
        catch (Exception e)
        {

        }
        return chaine;

        }
    }

The problem doesn't come from my .txt or the label because when I do the exact same thing (setting my text without setting in a picture), it works without a square.

Actually it's pretty strange because:

Jlabel + this method to set .txt: no square

Jlabel + picture + this method: square

Jlabel + picture: no square

Jlabel + picture + set.Text("Hello!"): square

Thanks in advance.

PS : I used a Jlabel instead of a JTextPane because I needeed transparency.

Was it helpful?

Solution

More than likely that this is because the file is not in the charset that Java expects. The InputStreamReader constructor can accept a Charset to bypass the platform default. Using

Charset.forName(...) 

as an argument you can try different ones and see if something works (the standard charsets can be found in the javadoc).

Alternatively, you can investigate this issue with Notepad++ under windows (and tons of other editors, just my personal favorite) to display your text with different charsets and convert if necessary.

The default charset of the JVM is returned by

Charset.defaultCharset()

with some caveats depending on your version (see this question).

OTHER TIPS

I finally discovered what was wrong.

When I delete the line which set the font tahoma, it works without a square !! I don't know why but this font doesn't allow me to set a picture and a text in my JLabel.

I think that you gave the correct answer but I didn't find anything wrong with my .txt in notepad++

Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top