Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-07-2010, 03:05 PM
Member
 
Join Date: Feb 2010
Posts: 5
Rep Power: 0
TheRealHoff is on a distinguished road
Default Reading in a text file
Hello,

I am trying to create a Java interface for reading text files. I have already managed to create a GUI which can read a text file into a JTextPane which is inside a JScrollPane, but now I want to do something different. Rather than scrolling through the text file, I want to be able to read through it like a book, splitting the file into pages and having "previous page" and "next page" buttons to cycle through. What I don't know how to do is how to read in say the first 200 characters of the text file and display them, then when next page is clicked, the next 200 characters should be displayed and so on, like pages.

Can anyone shed any light on how to do this? It would be much appreciated!

Craig
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-07-2010, 07:39 PM
Dumisan's Avatar
Member
 
Join Date: Feb 2010
Location: Romania
Posts: 30
Rep Power: 0
Dumisan is on a distinguished road
Default
I'm not sure what exactly your program should do but you can use the CardLayout to create the pages like this:

import java.applet.*;
import java.awt.*;
public class test extends Applet {
CardLayout layout = new CardLayout();
Panel card_panel = new Panel();
Panel button_panel = new Panel();

public void init() {
setLayout(new BorderLayout());
button_panel.add(new Button("First"));
button_panel.add(new Button("Prev"));
button_panel.add(new Button("Next"));
button_panel.add(new Button("Last"));
add("North", button_panel);
card_panel.setLayout(layout);
card_panel.add("card1", new Label("This is page1");
//you can add a Frame in stand of Label and allso add as many as you need
}
public boolean action (Event evt, Object arg) {
if(evt.target instanceof Button) {
if(arg.equals("First")) layout.first(card_panel);
else if(arg.equas("Prev")) layout.previous(card_panel);
else if(arg.equas("Next")) layout.next(card_panel);
else if(arg.equas("Last")) layout.last(card_panel);
return true;
}
return false;
}
}

I hope it helps
__________________
silence i'm trying to meditate
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-07-2010, 08:24 PM
Member
 
Join Date: Feb 2010
Posts: 5
Rep Power: 0
TheRealHoff is on a distinguished road
Default
Thanks very much for your reply

I'm more looking for how I could read the first, say, 200 characters from a text file and display those in a JTextPane, then when the "next page" button is pressed, I would like to read in the next 200 characters and display them in the same JTextPane.

Any ideas as to how I would do that?

Cheers,
Craig
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-07-2010, 08:42 PM
Dumisan's Avatar
Member
 
Join Date: Feb 2010
Location: Romania
Posts: 30
Rep Power: 0
Dumisan is on a distinguished road
Default
It's still not clear for me what exactly your cod should do so i'll put some questions

-where you tack your text from and where you save it
-this 200 characters restriction for page is something to be done manual or automatic when the text is taken over
__________________
silence i'm trying to meditate
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-07-2010, 09:08 PM
Member
 
Join Date: Feb 2010
Posts: 5
Rep Power: 0
TheRealHoff is on a distinguished road
Default
The text is coming from a file on my hard drive, its a file that contains an ebook in txt format. I can get the ebook displaying in a jScrollPane but I also wanted a page by page display, so I would need a limit on the number of characters that are displayed on one page. You see what I mean now?

Thanks for your help
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-07-2010, 09:33 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,431
Rep Power: 11
Fubarable is on a distinguished road
Default
I don't write programs for this and am not a professional programmer, but I imagine that you'll need some sort of buffer to read the text into, that the buffer would be large enough to hold many pages of text but small enough so that each read to the buffer doesn't take too much time, and that you'd fetch the text from the buffer to fill your page, that when the page text comes close to the end of the buffer, you'd read more in. The buffer could be an ArrayList<String>.

A BufferedReader can read in lines of text via its readline method. It maintains a pointer to the location in the file that was last read in (as do all other readers) and can be used for your purposes. I have not used NIO so I don't know how this is used here, but I imagine it could be used as well.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-07-2010, 09:35 PM
Dumisan's Avatar
Member
 
Join Date: Feb 2010
Location: Romania
Posts: 30
Rep Power: 0
Dumisan is on a distinguished road
Default
yes i figured that out but in your jScrollPane where you put the text in? a jLabel, a jTextField or a JTextArea ....
you can;t just put a text in a panel
and yet the ideea is what is the action event for the text splitting ... a button action , a key action , mouse .... your program won't do a thing unles you tell him what to do

btw if you use the NetBeans IDE you maybe have some issues with modifying the generated cod
__________________
silence i'm trying to meditate
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-07-2010, 11:04 PM
Member
 
Join Date: Feb 2010
Posts: 5
Rep Power: 0
TheRealHoff is on a distinguished road
Default
Yeah a buffer was my idea as well but Im really not sure how I would implement it :S and I'm reading the text into a JTextPane, so as I explained I want the first 200 or so characters read in, then when the user clicks the "next page" button it will display the next 200 characters. I guess a buffer could be set up with a size of 200, then flushed so the next 200 could be read in? But how would I implement that, I've looked for ages on the net!!!

Thanks again
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 02-07-2010, 11:15 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,431
Rep Power: 11
Fubarable is on a distinguished road
Default
Originally Posted by TheRealHoff View Post
Yeah a buffer was my idea as well but Im really not sure how I would implement it :S and I'm reading the text into a JTextPane, so as I explained I want the first 200 or so characters read in, then when the user clicks the "next page" button it will display the next 200 characters. I guess a buffer could be set up with a size of 200, then flushed so the next 200 could be read in? But how would I implement that, I've looked for ages on the net!!!

Thanks again
No, you don't want a buffer that small. You want the buffer to hold perhaps 100 pages of info or so, or whatever is a reasonable amount without either taking too long or too much memory, and then swap text in from the buffer to your page. You'll also want to do your text IO in a background thread such as a SwingWorker thread so as not to tie up the Event Dispatch Thread or EDT, Swing's main thread.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 02-07-2010, 11:28 PM
Member
 
Join Date: Feb 2010
Posts: 5
Rep Power: 0
TheRealHoff is on a distinguished road
Default
So if I read the whole text file into a buffer and then loop through it character by character, displaying the characters until a counter or something reaches 200???
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 02-07-2010, 11:47 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,431
Rep Power: 11
Fubarable is on a distinguished road
Default
Again, I'm no pro at this, but if it were my project, I'd experiment with things a bit and see how it worked, and then if I ran into a snag, would post a small demo program and my problem here.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
e-books, jtextpane, swing

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading text file trofyscarz New To Java 1 02-05-2010 02:24 AM
Reading a text file diegosened New To Java 4 01-15-2010 11:32 PM
Reading text file into an array Mahesh_ps New To Java 1 10-09-2009 03:04 PM
Reading two text file and sum them up matt_well New To Java 36 07-22-2008 02:55 AM
Reading text file Lennon-Guru New To Java 1 12-15-2007 11:38 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 05:23 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org