This question has been pending with a Proposed Answer for a long time, please accept it if it helped you to grade and close it, if not, please reject it and add comments as to what else is needed.
IF NO RESPONSE IN 4 DAYS, PLEASE LET ME KNOW AND I WILL FINALIZE THIS. EXPERT INPUT ALWAYS WELCOME, TO ENSURE THAT THE FAIR OUTCOME RESULTS AND QUALITY ITEMS ARE MOVED TO OUR PAQ.
Thank you,
Moondancer - EE Moderator
Main Topics
Browse All Topics





by: sey7Posted on 2000-11-06 at 02:46:42ID: 5119541
Extend your tree using the following class, where you have a print() method:
odel m){
ger(this). setDoubleB ufferingEn abled(fals e); Height); eight); NumPages); ("No such page(s)!"); leX(), pf.getImageableY());
b();
tion(); ()) { um.nextEle ment();
import java.io.IOException;
import java.awt.*;
import java.awt.geom.*;
import java.awt.Graphics2D;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.tree.*;
public class PrintableTree extends JTree implements Printable{
public PrintableTree(DefaultTreeM
super(m);
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black);
RepaintManager.currentMana
Dimension d = this.getSize();
double panelWidth = d.width;
double panelHeight = d.height;
double pageHeight = pf.getImageableHeight();
double pageWidth = pf.getImageableWidth();
double scale = pageWidth/panelWidth;
scale = scale > 0.7 ? 0.7:scale;
int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
//System.out.println(panel
//System.out.println(pageH
//System.out.println(total
if(pageIndex >= totalNumPages) {
//sec.util.Disp.printError
return Printable.NO_SUCH_PAGE;
}
// Shift Graphic to line up with beginning of print-imageable region
g2.translate(pf.getImageab
// Shift Graphic to line up with beginning of next page to print
g2.translate(0f, -pageIndex * pageHeight);
// Scale the page so the width fits...
g2.scale(scale, scale);
this.paint(g2); //repaint the page for printing
return Printable.PAGE_EXISTS;
}
public void printContent(){
PrinterJob pj=PrinterJob.getPrinterJo
pj.setPrintable(this);
if (pj.printDialog())
try{
pj.print();
} catch (Exception PrintException) {}
}
}
Before print() your tree expand the whole tree using:
public void expandTree()
{
int rowCount = 0;
Enumeration enum = m_RootNode.preorderEnumera
while(enum.hasMoreElements
rowCount++;
DefaultMutableTreeNode node = (DefaultMutableTreeNode)en
}
for(int row = 0; row < rowCount; row++) {
TreePath path = m_tree.getPathForRow(row);
if(path != null) {
m_tree.expandPath(path);
}
}
}
Of course, you can make it different, but now you have the methods for printing and expanding.