AllExperts > Experts 
Search      

Java

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Artemus Harper
Expertise
I have a BS in computer science and am working towards a Masters degree.

Experience
I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection, and Java java.nio.* knowledge of Java bytecode and annotations. Basics in c++ and c#
 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > printing a panel

Topic: Java



Expert: Artemus Harper
Date: 6/14/2008
Subject: printing a panel

Question
QUESTION: I am currently writing a piece of software, a sort of database type thing. I have a form which displays 10 random entries in the this database on one panel. It displays them using an array of text boxes (as this was the easiest way to populate 30 different text boxes (10 pieces with 3 pieces of data each)).

There is a lot of information on the panel but i see no reason why it wouldnt fit on one A4 piece of paper, I have already tried to print it myself using various techniques but all i seem to be able to do is print the very first label that is on the panel, everything else just isnt there.

I am using NetBeans IDE 6.0 and I am using its built in form WYSIWYG editor (this is mainly because there is a lot of forms and i couldn't be bothered fiddling around with coding it all out). I feel this may be a small problem with it as the labels were made using the editor but the JTextFields were coded in (as you cannot create arrays of text boxes in the editor).

I would attach my code for this certain class however it is 949 lines long :/ so im not sure you want that much code. However here is the code i used to create the text boxes.
----------------------
int vert = 0;
       for(int i=0; i<10; i++) {
           display[i] = new JTextField("");
           display[i].setEditable(false);
           display[i].setBounds(90,10+vert,105,20);
           description[i] = new JTextField("");
           description[i].setEditable(false);
           description[i].setBounds(275,10+vert,303,20);
           stock[i] = new JTextField("");
           stock[i].setEditable(false);
           stock[i].setBounds(643,10+vert,40,20);
           panelBorder.add(display[i]);
           panelBorder.add(description[i]);
           panelBorder.add(stock[i]);
           vert = vert + 32;
       }
---------------------

i dont think that will help much but the panel i need to print is 'panelBorder'.
if you would like me to send the class to you then i can do.

Thankyou for any help :]

ANSWER: Its hard to say what problems you are having unless you clarify what you mean by "various techniques".  Usually you would simply get a PrintJob, use setPrintable on a Printable object you create, which you simply call print on the component.

---------- FOLLOW-UP ----------

QUESTION: One way was that i used a PrintUtilities class

--------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class PrintUtilities implements Printable {
 private Component componentToBePrinted;

 public static void printComponent(Component c) {
   new PrintUtilities(c).print();
 }
 
 public PrintUtilities(Component componentToBePrinted) {
   this.componentToBePrinted = componentToBePrinted;
 }
 
 public void print() {
   PrinterJob printJob = PrinterJob.getPrinterJob();
   printJob.setPrintable(this);
   if (printJob.printDialog())
     try {
       printJob.print();
     } catch(PrinterException pe) {
       System.out.println("Error printing: " + pe);
     }
 }

 public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
   if (pageIndex > 0) {
     return(NO_SUCH_PAGE);
   } else {
     Graphics2D g2d = (Graphics2D)g;
     g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
     disableDoubleBuffering(componentToBePrinted);
     componentToBePrinted.paint(g2d);
     enableDoubleBuffering(componentToBePrinted);
     return(PAGE_EXISTS);
   }
 }

 public static void disableDoubleBuffering(Component c) {
   RepaintManager currentManager = RepaintManager.currentManager(c);
   currentManager.setDoubleBufferingEnabled(false);
 }

 public static void enableDoubleBuffering(Component c) {
   RepaintManager currentManager = RepaintManager.currentManager(c);
   currentManager.setDoubleBufferingEnabled(true);
 }
}
----------------------------

I called the PrintUtilities.printComponent(panelBorder);
but like i say it only printed the firt label on the panel.

Answer
I tried your PrintUtilties class with some sample code below:

import javax.swing.*;
import java.awt.*;
public class Test
{
  public static void main(String[] args)
  {
     JFrame frame = new JFrame("test");
     frame.setContentPane(new Box(BoxLayout.Y_AXIS));
     for(int i = 0; i < 5; i++)
     {
         Box rowBox = new Box(BoxLayout.X_AXIS);
         frame.add(rowBox);
         for(int j = 0; j < 5; j++)
             rowBox.add(new JLabel("["+i+","+j+"]"));
     }
     frame.pack();
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     frame.setVisible(true);
     PrintUtilities.printComponent(frame.getContentPane());
  }
}

and at least using a pdf printer it works fine. You could try changing:
componentToBePrinted.paint(g2d);
to
componentToBePrinted.print(g2d);

which might work for you. This method disables double buffering for you anyways. You can also try and see if drawing the component to a BufferedImage and showing that gives you the intended result.

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.