05. oktober 2003 - 17:34Der er
6 kommentarer og 1 løsning
Print af ny linje?
Når jeg tager noget data fra en JTextArea og via graphics printer det i java, så kommer nye linjer ikke med. Det bliver en lang linje jeg får printet. Er der nogen der har en løsning til dette?
Jeg har nogle gange brugt denne klasse, når jeg skulle udskrive. Den bruger dog JEditorPane istedet for JTextArea... Men så kan den også udskrive al formateringen af teksten...
/* The constructor initializes the pFormat and PJob variables. */ public DocumentRenderer() { pFormat = new PageFormat(); pJob = PrinterJob.getPrinterJob(); }
/* Method to get the current Document */ public Document getDocument() { if (jeditorPane != null) return jeditorPane.getDocument(); else return null; }
/* Method to get the current choice the width scaling option. */ public boolean getScaleWidthToFit() { return scaleWidthToFit; }
/* pageDialog() displays a page setup dialog. */ public void pageDialog() { pFormat = pJob.pageDialog(pFormat); }
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { double scale = 1.0; Graphics2D graphics2D; View rootView; // I graphics2D = (Graphics2D) graphics; // II jeditorPane.setSize((int) pageFormat.getImageableWidth(),Integer.MAX_VALUE); jeditorPane.validate(); // III rootView = jeditorPane.getUI().getRootView(jeditorPane); // IV if ((scaleWidthToFit) && (jeditorPane.getMinimumSize().getWidth() > pageFormat.getImageableWidth())) { scale = pageFormat.getImageableWidth()/ jeditorPane.getMinimumSize().getWidth(); graphics2D.scale(scale,scale); } // V graphics2D.setClip((int) (pageFormat.getImageableX()/scale), (int) (pageFormat.getImageableY()/scale), (int) (pageFormat.getImageableWidth()/scale), (int) (pageFormat.getImageableHeight()/scale)); // VI if (pageIndex > currentPage) { currentPage = pageIndex; pageStartY += pageEndY; pageEndY = graphics2D.getClipBounds().getHeight(); } // VII graphics2D.translate(graphics2D.getClipBounds().getX(), graphics2D.getClipBounds().getY()); // VIII Rectangle allocation = new Rectangle(0, (int) -pageStartY, (int) (jeditorPane.getMinimumSize().getWidth()), (int) (jeditorPane.getPreferredSize().getHeight())); // X if (printView(graphics2D,allocation,rootView)) { return Printable.PAGE_EXISTS; } else { pageStartY = 0; pageEndY = 0; currentPage = -1; return Printable.NO_SUCH_PAGE; } }
/* print(HTMLDocument) is called to set an HTMLDocument for printing. */ public void print(HTMLDocument htmlDocument) { setDocument(htmlDocument); printDialog(); }
/* print(JEditorPane) prints a Document contained within a JEDitorPane. */ public void print(JEditorPane jedPane) { setDocument(jedPane); printDialog(); }
/* print(PlainDocument) is called to set a PlainDocument for printing. */ public void print(PlainDocument plainDocument) { setDocument(plainDocument); printDialog(); }
/* A protected method, printDialog(), displays the print dialog and initiates printing in response to user input. */ protected void printDialog() { if (pJob.printDialog()) { pJob.setPrintable(this,pFormat); try { pJob.print(); } catch (PrinterException printerException) { pageStartY = 0; pageEndY = 0; currentPage = -1; System.out.println("Error Printing Document"); } } }
if (view.getViewCount() > 0) { for (int i = 0; i < view.getViewCount(); i++) { childAllocation = view.getChildAllocation(i,allocation); if (childAllocation != null) { childView = view.getView(i); if (printView(graphics2D,childAllocation,childView)) { pageExists = true; } } } } else { // I if (allocation.getBounds().getMaxY() >= clipRectangle.getY()) { pageExists = true; // II if ((allocation.getBounds().getHeight() > clipRectangle.getHeight()) && (allocation.intersects(clipRectangle))) { view.paint(graphics2D,allocation); } else { // III if (allocation.getBounds().getY() >= clipRectangle.getY()) { if (allocation.getBounds().getMaxY() <= clipRectangle.getMaxY()) { view.paint(graphics2D,allocation); } else { // IV if (allocation.getBounds().getY() < pageEndY) { pageEndY = allocation.getBounds().getY(); } } } } } } return pageExists; }
/* Method to set the content type the JEditorPane. */ protected void setContentType(String type) { jeditorPane.setContentType(type); }
/* Method to set an HTMLDocument as the Document to print. */ public void setDocument(HTMLDocument htmlDocument) { jeditorPane = new JEditorPane(); setDocument("text/html",htmlDocument); }
public void setDocument(JEditorPane jedPane) { jeditorPane = new JEditorPane(); setDocument(jedPane.getContentType(),jedPane.getDocument()); }
/* Method to set a PlainDocument as the Document to print. */ public void setDocument(PlainDocument plainDocument) { jeditorPane = new JEditorPane(); setDocument("text/plain",plainDocument); }
/* Method to set the content type and document of the JEditorPane. */ protected void setDocument(String type, Document document) { setContentType(type); jeditorPane.setDocument(document); }
/* Method to set the current choice of the width scaling option. */ public void setScaleWidthToFit(boolean scaleWidth) { scaleWidthToFit = scaleWidth; } }
mikkelbm det er tekst jeg henter fra database sammen med alt andet data og skriver til Graphics før jeg printer Graphics ud så jeg kan ikke rigtig bruge den kode.
Wow, the above seemed really complicated too.. Just put it in html, then it will linebreak automatically. example: "<html>text that you want in textarea</html>" or maybe in your case "<html> + sql-thing + </html>" You can also insert linebreaks manually when it's HTML by using <br>.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.