Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: diplay a large text in JTextArea (>2GB)

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default diplay a large text in JTextArea (>2GB)

    Default diplay a large text in JTextArea (>2GB)

    Hi All,

    I trying to display a text in JTextArea.
    For example :I want to read 23 lines. In my JTextArea I read only the first 10 lines. By moving the scrollbar I obtains the following 10 lines. And at the end 3 last ones. Nevertheless, I should be able to return behind.
    All the file must be present in JTextArea.

    Here 's my code. If somebody has an idea.
     private String string;
        	public MyDocument() {
        		try {
        			int offset = 0;
        			int x = 0;
        			for (int i = 0; i < 1024; i++) {
        				string = UUID.randomUUID().toString() + "\n";
        				if ((string != null) && (x < 10)) {
        					x++;
        				}
        				insertString(offset, string, null);
        				offset += string.length();
        			}
        		} catch (BadLocationException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		}
        	}
     
        	@Override
        	public String getText(int offset, int length) throws BadLocationException {
        		System.out.println(String.format("getText: Offset %d / Length: %d",
        				offset, length));
     
        		return super.getText(offset, length);
        	}
     
        	@Override
        	public void getText(int offset, int length, Segment seg)
        			throws BadLocationException {
        		System.out.println(String.format(
        				"getTextWithSegment: Offset %d / Length: %d", offset, length));
        		super.getText(offset, length, seg);
        	}
     
        	@Override
        	public int getLength() {
        		int length = super.getLength();
        		System.out.println("getLength: " + length);
        		return length;
        	}


    my main class:
        public GetTextClasse() {
        		JTextArea textArea = new JTextArea(new MyDocument());
        		setLayout(new GridLayout(1, 1));
     
        		add(new JScrollPane(textArea));
     
        	}
     
        	public static void main(String[] args) {
        		JFrame frame = new JFrame();
        		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		frame.setSize(800, 600);
        		frame.getContentPane().add(new GetTextClasse());
        		frame.setVisible(true);
     
        	}

    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: diplay a large text in JTextArea (>2GB)

    The code you posted won't compile. Can you make a small, complete program that will compile and execute and demonstrate your problem.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: diplay a large text in JTextArea (>2GB)

    ok,
    package guiTest;

    import java.util.UUID;

    import javax.swing.text.BadLocationException;
    import javax.swing.text.PlainDocument;
    import javax.swing.text.Segment;

    public class MyDocument extends PlainDocument{


    /**
    *
    */
    private static final long serialVersionUID = 7224102265305934163L;
    private String string;
    MyDocument(){

    try {
    int offset = 0;
    int x = 0;
    for (int i = 0; i < 1024; i++) {
    string = UUID.randomUUID().toString() + "\n";
    if ((string != null) && (x < 10)) {
    x++;
    }
    insertString(offset, string, null);
    offset += string.length();

    }
    } catch (BadLocationException e) {
    e.printStackTrace();
    }
    }

    @Override
    public String getText(int offset, int length) throws BadLocationException {
    // TODO Auto-generated method stub
    System.out.println(String.format("getText: Offset %d / Length: %d",
    offset, length));
    return super.getText(offset, length);

    }

    @Override
    public void getText(int offset, int length, Segment txt)
    throws BadLocationException {
    // TODO Auto-generated method stub
    System.out.println(String.format(
    "getTextWithSegment: Offset %d / Length: %d", offset, length));
    super.getText(offset, length, txt);
    }

    @Override
    public int getLength() {
    // TODO Auto-generated method stub
    int lenght = super.getLength();
    System.out.println("getLenght:" + lenght);
    return lenght;
    }
    }
    main class :
    package guiTest;

    import java.awt.GridLayout;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;

    public class TestGui extends JPanel {

    /**
    *
    */
    private static final long serialVersionUID = 6547379747523116652L;

    TestGui(){
    JTextArea textArea = new JTextArea(new MyDocument());
    setLayout(new GridLayout(1,1));

    add(new JScrollPane(textArea));
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new JFrame("test personel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.getContentPane().add(new TestGui());
    frame.setSize(400, 300);
    frame.setVisible(true);

    }

    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: diplay a large text in JTextArea (>2GB)

    What is supposed to happen when I execute your test program?
    I get a scrollable area with lots of lines (1024) of data.

    I changed one line so I could see what lines were being displayed:

    string = "i=" + i + " " + UUID.randomUUID().toString() + "\n";

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: diplay a large text in JTextArea (>2GB)

    Anyone eva found a solution for this?

Similar Threads

  1. Trying to display text from file in JTextArea. Please Help.
    By rjdelight in forum Java Theory & Questions
    Replies: 7
    Last Post: June 29th, 2011, 06:10 AM
  2. Large Text file(1GB-10GB) Java Swing
    By fortune2k in forum AWT / Java Swing
    Replies: 4
    Last Post: March 27th, 2011, 02:54 PM
  3. How can I avoid JTextArea from grabbing focus upon setting text
    By Orit in forum Java Theory & Questions
    Replies: 4
    Last Post: October 4th, 2010, 06:17 AM
  4. How can I append text in JTextArea from another class
    By chikaman in forum AWT / Java Swing
    Replies: 2
    Last Post: December 10th, 2009, 10:26 AM
  5. exception while Read very large file > 300 MB
    By ps.ganesh in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 12th, 2009, 12:39 AM