JtextArea

Am using the code below to put the text in my JTextArea into a String array.

String[] textLines = text.getText().split(System.getProperty("line.separator"));

but the array textLines is empty, how comes?

[221 byte] By [musiigea] at [2007-11-27 10:40:34]
# 1

hi! another problem in jtextarea.

can you post your code?

Yannixa at 2007-7-28 19:07:33 > top of Java-index,Desktop,Core GUI APIs...
# 2

which code are u talking about?

This is the code that is supposed to load the JtextArea into the string array

String[] textLines = text.getText().split(System.getProperty("line.separator"));

but "textLines.Length = 1" regardless the content of the JTextArea

musiigea at 2007-7-28 19:07:33 > top of Java-index,Desktop,Core GUI APIs...
# 3

The problem is in the code that you are not posting. We can't guess why your code is not working.

To steal outright from master java / swing coder camickr:

Please post a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour. You can find out more about this construct here:

url http://homepage1.nifty.com/algafield/sscce.html

And don't forget to use the Code Formatting Tags so the code retains its original formatting. Read up on these here:

url http://forum.java.sun.com/help.jspa?sec=formatting

petes1234a at 2007-7-28 19:07:33 > top of Java-index,Desktop,Core GUI APIs...
# 4

here is my code

public class Print_multi implements Printable, ActionListener {

int[] pageBreaks; // array of page break line positions.

/* Synthesise some sample lines of text */

String[] textLines;

JTextArea text;

public Print_multi(JTextArea text1){

text = text1;

textLines = text.getText().split(System.getProperty("line.separator"));

}

public int print(Graphics g, PageFormat pf, int pageIndex)

throws PrinterException {

Font font = new Font("Serif", Font.PLAIN, 10);

FontMetrics metrics = g.getFontMetrics(font);

int lineHeight = metrics.getHeight();

int num_of_lines = textLines.length;

for(int tttt = 0; tttt < num_of_lines; tttt++){

System.out.print("jtext elements" + textLines[tttt]);

}

if (pageBreaks == null) {

//initTextLines();

int linesPerPage = (int)(pf.getImageableHeight()/lineHeight);

//int numBreaks = (text.getRows()-1)/linesPerPage;

int numBreaks = (num_of_lines - 1)/linesPerPage;

pageBreaks = new int[numBreaks];

for (int b=0; b<numBreaks; b++) {

pageBreaks = (b+1)*linesPerPage;

}

}

if (pageIndex > pageBreaks.length) {

return NO_SUCH_PAGE;

}

/* User (0,0) is typically outside the imageable area, so we must

* translate by the X and Y values in the PageFormat to avoid clipping

* Since we are drawing text we

*/

Graphics2D g2d = (Graphics2D)g;

g2d.translate(pf.getImageableX(), pf.getImageableY());

/* Draw each line that is on this page.

* Increment 'y' position by lineHeight for each line.

*/

for ( int ww = 0; ww < num_of_lines; ww++){

System.out.print(textLines[ww] + "\n");

}

int y = 0;

int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex-1];

int end= (pageIndex == pageBreaks.length)

? num_of_lines : pageBreaks[pageIndex];

for (int line=start; line<end; line++) {

y += lineHeight;

g.drawString(textLines[line], 0, y);

}

/* tell the caller that this page is part of the printed document */

return PAGE_EXISTS;

}

public void actionPerformed(ActionEvent e) {

PrinterJob job = PrinterJob.getPrinterJob();

job.setPrintable(this);

boolean ok = job.printDialog();

if (ok) {

try {

job.print();

} catch (PrinterException ex) {

}

}

}>

musiigea at 2007-7-28 19:07:33 > top of Java-index,Desktop,Core GUI APIs...
# 5

ya didn't read my link about code tags. Now your code post is messed up and nonfunctional (wonder why part of it is bold?). Please reread (or just read) that link and repost. We can't help you if you don't help us.

petes1234a at 2007-7-28 19:07:33 > top of Java-index,Desktop,Core GUI APIs...
# 6

Sorry man, I've just been stressed today. I found my error, I forgot to initialise the string array

musiigea at 2007-7-28 19:07:33 > top of Java-index,Desktop,Core GUI APIs...
# 7

JTextArea uses "\n" (linefeed) to separate lines, no matter what platform it's running on. If you're running this code on a Windows machine, you're trying to split on "\r\n", which is why you get the whole text back in a single-element array. (You can put carriage returns in a JTextArea, but they'll be ignored as far as rendering is concerned, and they can cause errors due to incorrect positions/character counts. The read() method supplied by JTextComponent converts all line separators in the file to linefeeds.)

uncle_alicea at 2007-7-28 19:07:33 > top of Java-index,Desktop,Core GUI APIs...