Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 20th, 2006, 01:41 AM
itel itel is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 33 itel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 7 m 9 sec
Reputation Power: 2
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();
}

}

}

Reply With Quote
  #2  
Old December 21st, 2006, 12:28 PM
daniel_g daniel_g is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 60 daniel_g User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 3 m 50 sec
Reputation Power: 3
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.

Reply With Quote
  #3  
Old December 22nd, 2006, 12:24 AM
itel itel is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 33 itel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 7 m 9 sec
Reputation Power: 2
Thanks a lot!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Need help: JTextPane


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT