JDK-4129248 : I can't print a JTree
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.5
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_95
  • CPU: x86
  • Submitted: 1998-04-15
  • Updated: 1998-05-11
  • Resolved: 1998-05-11
Related Reports
Duplicate :  
Description

Name: rk38400			Date: 04/15/98


The following source code contains the problem.
When I press the "Print" button a white sheet of paper is printed.
When I press the "Print all" button an exception whithout message is raised.
The line "Exception caught during printing. Exception = null
" is printed in the java console.

Is it a bug or where I mismatch ?

Thanks.

C. PIGNEROL (###@###.###)


The source code :

/** PrintTree.java.
This file contains the PrintTree's class, which enables users to
see config files.
This class is made with the JFC.
*/

// Gui support :
import java.awt.Frame;
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.sun.java.swing.JApplet;
import com.sun.java.swing.JButton;
import com.sun.java.swing.JLabel;
import com.sun.java.swing.JTree;
import com.sun.java.swing.tree.DefaultMutableTreeNode;
import com.sun.java.swing.tree.TreeNode;
import com.sun.java.swing.JScrollPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

// Printing support :
import java.awt.Toolkit;
import java.awt.Graphics;
import java.awt.PrintJob;


public final class PrintTree extends JApplet implements ActionListener
{
    public PrintTree ( )
    {
	super ( );
    }
    
    /** main. For using not in an Applet. */
    public static void main (String args [ ])
    {
	Frame		frame		= new Frame ("Tree Viewer");
	PrintTree	PrintTree	= new PrintTree ( );
	frame.add (PrintTree);
	PrintTree.init ( );
	frame.pack ( );
	frame.show ( );
    }
    
    public void init ( )
    {
	JButton		    button	    = null;
	JLabel		    label	    = null;
	final int	    colsNum = 6, rowsNum = 6;
	GridBagLayout	    gridbag	    = new GridBagLayout ( );
	GridBagConstraints  constraints	    = new GridBagConstraints ( );
	constraints.fill		    = GridBagConstraints.BOTH;
	getContentPane ( ).setLayout (gridbag);
	
	// The tree.
	tree		= new JTree ( );
	JScrollPane scrollPane	= new JScrollPane (tree);
	buildConstraints (constraints, 0, 0, colsNum, rowsNum - 2, 100, 100);
	gridbag.setConstraints (scrollPane, constraints);
	getContentPane ( ).add (scrollPane);
	
	// Last row : Print and Exit buttons.
	button	= new JButton ("Print view");
	buildConstraints (constraints, 0, rowsNum - 1, 1, 1, 100, 100);
	gridbag.setConstraints (button, constraints);
	getContentPane ( ).add (button);
	button.setActionCommand (button.getText ( ));
	button.addActionListener (this);
	button.setToolTipText ("Print the tree view.");
	button	= new JButton ("Print all");
	buildConstraints (constraints, 1, rowsNum - 1, 1, 1, 100, 100);
	gridbag.setConstraints (button, constraints);
	getContentPane ( ).add (button);
	button.setActionCommand (button.getText ( ));
	button.addActionListener (this);
	button.setToolTipText ("Print all the gui.");
	button	= new JButton ("Exit");
	buildConstraints (constraints, colsNum - 1, rowsNum - 1, 1, 1, 100, 100);
	gridbag.setConstraints (button, constraints);
	getContentPane ( ).add (button);
	button.setActionCommand (button.getText ( ));
	button.addActionListener (this);
	button.setToolTipText ("Exit from this application.");
    }
    
    private void print (int what)   // what = PRINT_VIEW or PRINT_ALL
    {
	try
	{
	    //Toolkit	tk	= Toolkit.getDefaultToolkit ( );
	    
	    Frame	parent	= (Frame)getParent ( );
	    Toolkit	tk	= getToolkit ( );
	    PrintJob	pjob	= tk.getPrintJob (parent, "PrintTree", null);
	    
	    if (null != pjob)
	    {
		Graphics    g	= pjob.getGraphics ( );
		
		if (null != g)
		{
		    if (PRINT_VIEW == what)
		    {
			System.out.println ("Printing document ...");
			tree.printAll (g);
			//tree.paint (g);
		    }
		    else
		    {
			System.out.println ("Printing gui ...");
			parent.printAll (g);
			//parent.paint (g);
		    }

		    g.dispose ( );	// Flush page.
		    pjob.end ( );
		    System.out.println ("Printing succeeded.");
		}
		else
		    System.out.println ("Graphics nul.");
	    }
	    else
	    {
		System.out.println ("Printing canceled.");
	    }
	}
	catch (Exception e)
	{
	    System.out.println ("Exception caught during printing. Exception = " + e.getMessage ( ));
	}
    }
    
    private void exit ( )
    {
	System.exit (0);
    }
    
    public void actionPerformed (ActionEvent e)
    {
	String	action	= e.getActionCommand ( );
	
	if (action.equals ("Print view"))
	    print (PRINT_VIEW);
	else if (action.equals ("Print all"))
	    print (PRINT_ALL);
	else if (action.equals ("Exit"))
	    exit ( );
    }

    /** buildConstraints. Create a constain. cf. Java 1.1 en 21 jours, page 324. */
    private void buildConstraints (GridBagConstraints gbc, int gx, int gy,
                                   int gw, int gh, double wx, double wy)
    {
    	gbc.gridx		= gx;
    	gbc.gridy		= gy;
    	gbc.gridwidth		= gw;
    	gbc.gridheight		= gh;
    	gbc.weightx		= wx;
    	gbc.weighty		= wy;
    }
    
    private final   int		    PRINT_VIEW		= 0;
    private final   int		    PRINT_ALL		= 1;
    private	    JTree	    tree;
}
(Review ID: 27747)
======================================================================

Comments
EVALUATION This is a general problem with JComponents, and is a duplicate of another bug. sky 1998-05-11
11-06-2004

WORK AROUND Name: rk38400 Date: 04/15/98 I don't now what is a workaround. Sorry ! ======================================================================
11-06-2004

PUBLIC COMMENTS This is a general problem with JComponents, and is a duplicate of another bug. sky 1998-05-11
10-06-2004