Avatar billede aslan Nybegynder
05. oktober 2003 - 17:34 Der 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?
Avatar billede togsverd1985 Praktikant
05. oktober 2003 - 18:17 #1
skriv lige koden
Avatar billede mikkelbm Nybegynder
05. oktober 2003 - 19:54 #2
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...

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JEditorPane;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import javax.swing.text.View;
import javax.swing.text.html.HTMLDocument;

public class DocumentRenderer implements Printable {

  protected int currentPage = -1;              //Used to keep track of when
                                                //the page to print changes.

  protected JEditorPane jeditorPane;            //Container to hold the
                                                //Document. This object will
                                                //be used to lay out the
                                                //Document for printing.

  protected double pageEndY = 0;                //Location of the current page
                                                //end.

  protected double pageStartY = 0;              //Location of the current page
                                                //start.

  protected boolean scaleWidthToFit = true;    //boolean to allow control over
                                                //whether pages too wide to fit
                                                //on a page will be scaled.

  protected PageFormat pFormat;
  protected PrinterJob pJob;

/*  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");
      }
    }
  }

  protected boolean printView(Graphics2D graphics2D, Shape allocation,
  View view) {
    boolean pageExists = false;
    Rectangle clipRectangle = graphics2D.getClipBounds();
    Shape childAllocation;
    View childView;

    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;
  }
}
Avatar billede mikkelbm Nybegynder
05. oktober 2003 - 19:55 #3
Så kalder du den bare:

DocumentRenderer DocumentRenderer = new DocumentRenderer();
DocumentRenderer.print(myTextPane);
Avatar billede aslan Nybegynder
05. oktober 2003 - 21:59 #4
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.
Avatar billede holmqvist Nybegynder
08. oktober 2003 - 22:11 #5
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>.
Avatar billede mikkelbm Nybegynder
20. november 2003 - 21:48 #6
Er du kommet videre???
Avatar billede aslan Nybegynder
20. november 2003 - 22:01 #7
ja og det var ikke jeres forslag jeg brugte..
Avatar billede Ny bruger Nybegynder

Din løsning...

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester