help: JTextPane

I need to add text from file (12.txt - any text data here) into JTextPane. Here is code. If I print like: System.out.println, then works good, but into the JTextPane is only the last line. Pleasse help.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

public class One {

public String filename = new String();

public static JTextPane prikaz = new JTextPane ();

public One (){

JFrame window = new JFrame ("dddsds");

Container vsebnik = window.getContentPane();

vsebnik.add (prikaz, BorderLayout.CENTER);

window. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setSize(600, 500);

window.setVisible (true);

}

public static void main (String args[]){

try {

UIManager.setLookAndFeel (

UIManager.getCrossPlatformLookAndFeelClassName() );

}

catch (Exception k) {}

One mfc = new One();

readMyFile();

}

static void readMyFile() {

String record = null;

try {

FileReader fr = new FileReader("12.txt");

BufferedReader br = new BufferedReader(fr);

record = new String();

while ((record = br.readLine()) != null) {

System.out.println(record);

prikaz.setText(record);

}

} catch (IOException l) {

// catch possible io errors from readLine()

System.out.println("Uh oh, got an IOException error!");

l.printStackTrace();

}

}

}

[1511 byte] By [itelitela] at [2007-11-26 13:15:17]
# 1

record = new String();

while ((record = br.readLine()) != null) {

System.out.println(record);

prikaz.setText(record);

}

The problem is here. setText wipes out what was previously there, so only the last line will show.

try using

prikaz.replaceSelection(record);

instead.

BTW,

record = new String();

is just a waste of time. the new String you create is never used and just garbage collected. because

record = br.readLine()

creates it's own String.

Anytime you find yourself doing new String(); you are probably doing something wrong.

dmbdmba at 2007-7-7 17:35:56 > top of Java-index,Java Essentials,New To Java...