|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help: JTextPane
I need to add text from file (12.txt) 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(); } } } |
|
#2
|
|||
|
|||
|
Code:
while ((record = br.readLine()) != null) {
System.out.println(record);
prikaz.setText(record);
}
is equivalent to Code:
prikaz.setText(br.readLine()); prikaz.setText(br.readLine()); prikaz.setText(br.readLine()); prikaz.setText(br.readLine()); //... until last line. Each setText() erases the previous entry from the JTextPane. So what you really want to do is: Code:
prikaz.setText(br.readLine() + br.readLine() + br.readLine() + ...); So what you migth want to do, is use a stringbuffer: http://java.sun.com/j2se/1.4.2/docs...ringBuffer.html Please, read the java doc about string buffers before proceding, as this is something that you will need to use very often. Then you can change your code to: Code:
//Note: This Is Untested Code.
StringBuffer str = new StringBuffer();
while ((record = br.readLine()) != null) {
str.append(record); //if you read the doc, you should know what append() is doing
}
prikaz.setText(str);
That should solve your problem. Last edited by daniel_g : December 21st, 2006 at 12:30 PM. |
|
#3
|
|||
|
|||
|
Thanks a lot!
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Need help: JTextPane |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|