Results 1 to 11 of 11
  1. #1
    TheRealHoff is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    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

  2. #2
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    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:p

  3. #3
    TheRealHoff is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    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

  4. #4
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    It's still not clear for me what exactly your cod should do so i'll put some questions :D

    -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:p

  5. #5
    TheRealHoff is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    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 :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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.

  7. #7
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    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:p

  8. #8
    TheRealHoff is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote 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.

  10. #10
    TheRealHoff is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    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???

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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.

Similar Threads

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •