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.
Code :
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:
Code :
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.
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.
Re: diplay a large text in JTextArea (>2GB)
ok,
Quote:
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 :
Quote:
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);
}
}
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";