Only one character in a JTextPane using a DefaultStyledDocument
Heya,
I'm developing a program to read and filter some big traces file and I'm having a little problem now.
I've done a small text program, with the class MyFile, MyDocument, FileViewer and Test, I try to load myDocument in a JTextPane but I only see the first character of the document. Maybe I nedd to implement more function in MyDocument class, I've also read some thing about the Element but I'm a little loose, I don't know which way to choose for my test.
Hope you could help me,
Thanks.
[531 byte] By [
Nionoa] at [2007-10-2 18:23:39]

Here a part of my code FileViewer class
/*
* Set properties of the components
*/
mainWindowApplication.setTitle("Traces Restit Test");
mainWindowApplication.setSize(640, 480);
//tracesTextPane.setText("test");
//tracesTextPane.setContentType("DefaultStyledDocument");
tracesTextPane.setEditable(false);
tracesTextPane.getCaret().setVisible(true);
// Creation of the scrollable areas - ENTRAINE UN BUG ET NE FONCTIONNE PAS VRAIMENT
//scrollTraces = new JScrollPane(tracesTextPane,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) ;
loadFileButton.setText("Go test !");
colorLineButton.setText("Color 1/2 !");
clearButton.setText("Clear !");
exitButton.setText("Quit");
myDocument = GUI_myDocument;
/*
* Style, red letter
*/
final Style styleRed = tracesTextPane.addStyle("Red", null);
StyleConstants.setForeground(styleRed, Color.red);
StyleConstants.setFontSize(styleRed, 14);
/*
* Odd line will be in red, pair line are keeped in black
*/
for (int lineNumber = 0; lineNumber < myDocument.myFile.getNumberOfLines();
lineNumber++) {
if ((lineNumber % 2) == 0) {
// Get the line
int offsetBeginLine = myDocument.getOffsetBeginLine(lineNumber);
int offsetEndLine = myDocument.getOffsetEndLine(lineNumber);
int lengthLine = myDocument.getLengthLine(offsetBeginLine,
offsetEndLine);
// Odd line in red
myDocument.setCharacterAttributes(offsetBeginLine, lengthLine,
tracesTextPane.getStyle("Red"), true);
}
}
// TEST
//myDocument.setCharacterAttributes(0, 5,tracesTextPane.getStyle("Red"), true);
/*
* Initialisation of the reading task progressBar
*/
taskProgressBar = new JProgressBar(0);
taskProgressBar.setValue(0);
taskProgressBar.setStringPainted(true);
/*
* Specify LayoutManagers to arrange components in the containers
*/
mainWindowApplication.getContentPane().setLayout(new BorderLayout());
buttonPanel.setLayout(new FlowLayout());
tracesPanel.setLayout(new BorderLayout());
/*
* Add components to containers, layout constraints
*/
buttonPanel.add(taskProgressBar);
buttonPanel.add(loadFileButton);
buttonPanel.add(colorLineButton);
buttonPanel.add(clearButton);
buttonPanel.add(exitButton);
//tracesPanel.add(scrollTraces);
tracesPanel.add(tracesTextPane);
mainWindowApplication.getContentPane().add(tracesPanel, "Center");
mainWindowApplication.getContentPane().add(buttonPanel, "South");
Nionoa at 2007-7-13 19:44:32 >

the main class
MyFile myFile = new MyFile(
"R:\\v_TRC\\TracesRestit\\test\\IDT.output");
// Open the file to read
myFile.indexTab();
/*
* Windows Look and Feel
*/
if ((System.getProperty("os.name").indexOf("Windows") != -1) ||
(System.getProperty("os.name").indexOf("windows") != -1)) {
try {
// Look and feel Windows
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
// Path to the file
MyDocument myDocument = new MyDocument(
myFile);
// GUI
FileViewer myFileViewer = new FileViewer(myDocument);
MyDocument class
* This class is used to implement the Document interface
*/
public class MyDocument extends DefaultStyledDocument {
/**
* File to display
*/
MyFile myFile;
/**
* Constructor
*
* @param filePath
* @throws IOException
*/
public MyDocument(MyFile myFile) throws IOException {
this.myFile = myFile;
}
MyFile constructor
/**
* Used to show the performance of the memoryMappedFile
*
* @param String filePath
* @throws IOException
*/
public MyFile(String filePath) throws IOException {
// Perf Memory Begin
System.out.println("Begin ");
System.out.println(Runtime.getRuntime().totalMemory() + " - " +
Runtime.getRuntime().freeMemory());
// Get a channel for writing a random access file in reading "r" mode.
FileChannel rCh = (new RandomAccessFile(filePath, "r")).getChannel();
// Map entire file to memory and close the channel
fileSize = rCh.size();
buf = rCh.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
rCh.close();
// Perf Memory
System.out.println(Runtime.getRuntime().totalMemory() + " - " +
Runtime.getRuntime().freeMemory());
}
My main problem is that I only see one character in the JTextPane when I load the file.
Nionoa at 2007-7-13 19:44:32 >
