• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How can I print the values from a JTree to a paper

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JTree and I want to get the values from it and print it on a paper.The printing should be well formated.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what exactly is "well formatted"?
This looks like a basic IO issue. Read the JTree, format to an output file. You need a PrinterJob to get the output file to paper.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I have so far in a conversion of PrintJText (another post in JavaRanch) to being able to print any JComponent. It will print a page of a JTree but does not understand page size so far. I just started on this but it looks promising.
Joe
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintJComponent implements Printable {
private JComponent maJComponent;
public static void printComponent(JComponent maJComponent) {
new PrintJComponent(maJComponent).print();
}
public PrintJComponent(JComponent maJComponent) {
this.maJComponent = maJComponent;
}
public void print() {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(PrintJComponent.this);
try {
if (pj.printDialog())
pj.print();
}
catch (Exception PrintException) { }
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black);
int fontHeight = g2.getFontMetrics().getHeight();
int fontDesent = g2.getFontMetrics().getDescent();
double pageHeight = pageFormat.getImageableHeight() - fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double headerHeightOnPage = 16.5;
double oneRowHeight = fontHeight;
int totalNumPages = 1;
double pageHeightForTable = 648;
double tableWidth = 468;
double scale = 1;
int numRowsOnAPage = 60;
if(maJComponent instanceof JTextArea)
{
numRowsOnAPage = (int)((pageHeight - headerHeightOnPage) / oneRowHeight);
totalNumPages = (int)Math.ceil(((double)(((JTextArea)maJComponent).getLineCount()) / numRowsOnAPage));
System.out.println(totalNumPages);
pageHeightForTable = oneRowHeight * numRowsOnAPage;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
};
}
double tableWidthOnPage = tableWidth * scale;
if (pageIndex >= totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2.drawString("Page: " + (pageIndex + 1), (int)pageWidth / 2 - 35, (int)(pageHeight + fontHeight - fontDesent));
g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -pageIndex * pageHeightForTable);
if (pageIndex + 1 == totalNumPages) {
int lastRowPrinted = numRowsOnAPage * pageIndex;
int numRowsLeft = totalNumPages * numRowsOnAPage - lastRowPrinted;
g2.setClip(0, (int)(pageHeightForTable * pageIndex), (int)Math.ceil(tableWidthOnPage),
(int)Math.ceil(oneRowHeight * numRowsLeft));
}
else {
g2.setClip(0, (int)(pageHeightForTable * pageIndex), (int)Math.ceil(tableWidthOnPage),
(int)Math.ceil(pageHeightForTable));
}
g2.scale(scale, scale);
maJComponent.paint(g2);
g2.scale(1 / scale, 1 / scale);
g2.translate(0f, pageIndex * pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0, (int)Math.ceil(tableWidthOnPage), (int)Math.ceil(headerHeightOnPage));
g2.scale(scale, scale);
return Printable.PAGE_EXISTS;
}
}
 
sailsjam
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help.The help alot.
reply
    Bookmark Topic Watch Topic
  • New Topic