Question

I'm just a beginner in Java. Here's my problem :

I created a scrollable field of text with this code (1):

jZone_Text = new JTextPane();
scrollPane = new JScrollPane(jZone_Text);                               
jZone_Text = new JTextPane();                           
jPanelRecord.add(scrollPane);
scrollPane.setBounds(20, 70, 550, 190); 
scrollPane.setVisible(false);

Then I would like to get the text from my JTextPane and write it in a .txt file So I did this (2) :

try {   
    FileWriter fw = new FileWriter ("C:\\Users\\Admin\\Desktop\\memo.txt");
    BufferedWriter bw = new BufferedWriter (fw);
    PrintWriter fichierSortie = new PrintWriter (bw); 
    fichierSortie.println (jZoneText.getText()+"\n "); 
    fichierSortie.close();

    }catch (IOException e2){
    }

But when I try, it creates my memo, but it's blank.

I tried to use the (2) code, and asked to get the text from a textfield which is not in my scrollable pane and it works perfectly..

I think that I can't get the text from a textpane which is in my scrollPane... Is it possible ?

Was it helpful?

Solution

You are creating jZone_Text twice in (1). The second jZone_Text is not the one in the scroll pane thus it is empty. Replace with :

        jZone_Text = new JTextPane();
         scrollPane = new JScrollPane(jZone_Text);                              
         jPanelRecord.add(scrollPane);
         scrollPane.setBounds(20, 70, 550, 190);    
         scrollPane.setVisible(false);

Also, it is called jTitre_Text in (2), I'm assuming it's a typo

OTHER TIPS

You create an empty JTextPane()

Fill it with a string, and then use .getText() to get this "myString"

and btw: do not create a new JTextPane() in line 3 ;)

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