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]
# 1

> Hope you could help me,

With what?

We have absolutely no idea what your requirement is. "developoing a program to read and filter" does not describe your requirement. We have no idea what type of filtering you are doing or why you decided to use a JTextPane, or why you think you need to create a custom document. The easy answer would be to just delete the lines from the document that you don't want displayed.

We have no idea what you have done. The fact that it isn't working doesn't not provide us with any usefull information.

camickra at 2007-7-13 19:44:32 > top of Java-index,Desktop,Core GUI APIs...
# 2

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 > top of Java-index,Desktop,Core GUI APIs...
# 3

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 > top of Java-index,Desktop,Core GUI APIs...
# 4

I have absolutely no idea why you are trying to use a FileChannel to read the file into memory. The text pane will copy the data to a Document which stores the text in its own buffer. Why do you want to keep two copies of the file in memory.

Just use the textpane the way it was designed and you the read(...) method of the textpane to load the file into the Document.

camickra at 2007-7-13 19:44:32 > top of Java-index,Desktop,Core GUI APIs...
# 5

I've resolve my problem, it's because like I said in my first post I've to use the Element class to get all the "level" of the document.

I need to keep it in memory too.

I'll post some code if people are interested.

I've search things about the scrollbar on the forum, but everything I've try doesn't work, can you help me to add a vertical and horizontal scrollbar to my JTextPane ? I think I've to include my JtextPane in a JScrollPane but I don't find the solution.

Nionoa at 2007-7-13 19:44:32 > top of Java-index,Desktop,Core GUI APIs...