Results 1 to 13 of 13
- 10-08-2009, 09:05 AM #1
Problem with painting the contents of a file on a JTextArea
Hi friends!
Recently I am trying to implement this problem : Paint the contents of a file (text file) on to a JTextArea. For the first look, it seems to work well, but actually when I take a deeper look at the output, I realized that the contents of the file are not fully displayed on the JTextArea. That is, some parts of the text file are not displayed and some parts are displayed clearly.
After studying the code clearly, I cannot find out any problem and decide to get help from professional :mad:. Here is the code, please help me :mad:
Thank you!Java Code:import java.awt.*; import javax.swing.*; import java.io.*; public class TextAreaTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { TextAreaFrame frame = new TextAreaFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } catch(IOException e) { e.printStackTrace(); } } }); } } class TextAreaFrame extends JFrame { private JTextArea textArea; public TextAreaFrame() throws IOException { setTitle("JIE"); setSize(500, 400); textArea = new JTextArea(20, 60); textArea.setLineWrap(true); textArea.setEditable(false); textArea.setFont(new Font("Vrinda", Font.BOLD, 23)); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane); pack(); FileInputStream fis = new FileInputStream("haha.txt"); // display the contents of the file [B]haha.txt[/B] BufferedReader bd = new BufferedReader(new InputStreamReader(fis)); String line; while((line = bd.readLine()) != null) { textArea.append(bd.readLine()); textArea.append("\n"); } } }Last edited by Willi; 10-08-2009 at 09:09 AM.
- 10-08-2009, 09:37 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Don't use those streams for reading text data. Use FileReader/BufferedReader instead.
Alternatively, read the API specs for the JTextComponent.read method.
- 10-08-2009, 09:38 AM #3
Hi Willi,
you're skipping lines:So you should append line:Java Code:while((line = bd.readLine()) != null) // <-----read here { textArea.append(bd.readLine()); // <----read again textArea.append("\n"); }
And you should consider using a Scanner:Java Code:while((line = bd.readLine()) != null) // <-----read here { textArea.append(line); textArea.append("\n"); }
EDIT: sloooowJava Code:Scanner s = new Scanner(new File("haha.txt")); while (s.hasNext()) { textArea.append(s.nextLine()); textArea.append("\n"); }
- 10-08-2009, 12:56 PM #4
Great thanks!
hi r03198x : actually I used BufferedReader but it still does not work well. It just print a part of the file and some lines are lost. Can you explain for me why? thanks :D
to PhHein : the best way is to create an object of the class File to hold a file and then read from it using FileReader. Thanks for your reply :D
- 10-08-2009, 01:06 PM #5
- 10-08-2009, 01:36 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Please read the messages posted already.
- 10-08-2009, 02:40 PM #7
Sorry r035198x, I'm a bit dense today. Rereading the posted messages doesn't give me a clue.
- 10-08-2009, 03:11 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 10-08-2009, 03:12 PM #9
Great, just great. Sums up my day :)
- 10-08-2009, 06:16 PM #10
- 10-08-2009, 07:15 PM #11
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Did you use FileReader with it like I suggested?
Did you read the API specs for that read method like I suggested?
Did you read any of the responses posted in this thread?
- 10-09-2009, 05:42 AM #12
Hey easy brother! What happen to you? I try that program according to your advice, and here is the code, it works perfect.
Now i just want to ask why does BufferedReader not work as well as FileReader :(Java Code:import java.awt.*; import javax.swing.*; import java.io.*; public class TextAreaTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { TextAreaFrame frame = new TextAreaFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } catch(IOException e) { e.printStackTrace(); } } }); } } class TextAreaFrame extends JFrame { JTextArea textArea; public TextAreaFrame() throws IOException { setTitle("JIE"); setSize(500, 400); textArea = new JTextArea(20, 60); textArea.setLineWrap(true); textArea.setFont(new Font("Vrinda", Font.BOLD, 23)); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane); pack(); [B] // CODE CHANGED HERE File inputFile = new File("haha.txt"); FileReader fr = new FileReader(inputFile);[/B] int c; while((c = fr.read()) != -1) { textArea.read(fr, fr.toString()); textArea.append("\n"); } fr.close(); } }
- 10-09-2009, 06:26 AM #13
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
problem trying to display the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 17Last Post: 07-13-2009, 05:44 AM -
Jpanel painting problem
By kcakir in forum AWT / SwingReplies: 3Last Post: 04-15-2009, 10:21 PM -
problems trying to view the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 PM -
problem trying to view the contents of a text file in JTextArea
By warship in forum AWT / SwingReplies: 0Last Post: 07-17-2007, 03:30 PM -
viewing the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 0Last Post: 07-17-2007, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks