Results 1 to 10 of 10
- 05-30-2012, 05:48 AM #1
Senior Member
- Join Date
- Jan 2012
- Location
- TamilNadu
- Posts
- 162
- Rep Power
- 2
unable to print components in JPanel
Good Morning!
.gif)
I am new for this "Printing" section in java, by implementing Printable
i try to print all that components (JLabels) which are inside the JPanel
i can do it by the fallowing print methods, but it prints only the JPanel not with it's components..,
Anyone can help me to learn this section & what changes have to do with my code please,Java Code:public int print(Graphics gx, PageFormat pf, int page) throws PrinterException { if (page>0){return NO_SUCH_PAGE;} Graphics2D g = (Graphics2D)gx; g.translate(pf.getImageableX(), pf.getImageableY()); //panll.paintAll(gx); panll.paint(gx); return PAGE_EXISTS; }
Thank you..,
-
Re: unable to print components in JPanel
Printing can be pretty complex, and I recommend you simplify your problem as much as possible both for your benefit -- a simpler program is much easier to debug, and for ours -- if your program doesn't work, you can post it here, and we'll have a better chance of being able to give you good help. So on that note, consider creating and posting a small compilable and runnable program, one that requires no outside resources, one that we can compile and run unmodified on our computers with our Java compiler, that is small enough to be easy to read and understand quickly, and one that demonstrates your problem, in essence, an SSCCE.
- 05-30-2012, 06:21 AM #3
Senior Member
- Join Date
- Jan 2012
- Location
- TamilNadu
- Posts
- 162
- Rep Power
- 2
Re: unable to print components in JPanel
Thank you!
hear after i will fallow your instructions sir,
But! in my simple program, i unable to printing the components from the fallowing code that's why i need your help,
can you guide me please..,Java Code:package test; import java.awt.*; import java.awt.event.*; import java.awt.print.*; import java.awt.print.Printable; public class testis extends javax.swing.JFrame implements Printable { private Component mComponent; /** Creates new form testis */ public testis() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); mylb = new javax.swing.JLabel(); myb = new javax.swing.JButton(); panll = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); jLabel2.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel2.setText("Praise"); getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, 330, 70)); jLabel3.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel3.setText("The"); getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 100, 330, 70)); jLabel4.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel4.setText("Lord"); getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 190, 330, 80)); mylb.setText("Jesus Christ"); mylb.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true)); getContentPane().add(mylb, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 29, 328, 243)); myb.setText("Print"); myb.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { mybActionPerformed(evt); } }); getContentPane().add(myb, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1)); javax.swing.GroupLayout panllLayout = new javax.swing.GroupLayout(panll); panll.setLayout(panllLayout); panllLayout.setHorizontalGroup( panllLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 330, Short.MAX_VALUE) ); panllLayout.setVerticalGroup( panllLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 240, Short.MAX_VALUE) ); getContentPane().add(panll, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, 330, 240)); pack(); }// </editor-fold> private void mybActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); if (job.printDialog() == true) { try {job.print();} catch (PrinterException ex){ } } } public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(testis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(testis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(testis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(testis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new testis().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JButton myb; private javax.swing.JLabel mylb; private javax.swing.JPanel panll; // End of variables declaration public int print(Graphics gx, PageFormat pf, int page) throws PrinterException { if (page>0){return NO_SUCH_PAGE;} Graphics2D g = (Graphics2D)gx; g.translate(pf.getImageableX(), pf.getImageableY()); //panll.paintAll(gx); panll.paint(gx); return PAGE_EXISTS; } }
Thank you..,
-
Re: unable to print components in JPanel
You're ignoring an important exception above. I would suggest you don't do this. Also, please try to make your SSCCE shorter, and without NetBean's derived code.
-
Re: unable to print components in JPanel
Your code also looks different from the tutorials, in particular where you call panll.paint(gx); You should be calling something else. Please check this link to see what: Printing the Contents of a User Interface (The Java™ Tutorials > 2D Graphics > Printing)
- 05-30-2012, 08:13 AM #6
Senior Member
- Join Date
- Jan 2012
- Location
- TamilNadu
- Posts
- 162
- Rep Power
- 2
Re: unable to print components in JPanel
Sorry for my mistake!
AND Thanks to give a way by your link sir..,
- 06-01-2012, 08:51 AM #7
Senior Member
- Join Date
- Jan 2012
- Location
- TamilNadu
- Posts
- 162
- Rep Power
- 2
Re: unable to print components in JPanel
Happy To Meet You!
.gif)
I try with that tutorial it is so helpful , and now try it by using PrintUtilities class for simplify Printing the Components sir..,
the code for PrintUtilities class is
Java Code: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); } }
Now it is enough to pass it
as i said i am new for this section, by your guide i learn the basics. But now also i face the same.,Java Code:PrintUtilities.printComponent(panll);
I NEED your help now..,
it prints the panel only, what i have to do to print the JLabel which are placed on that JPanel sir..,
I am waiting For your response please..,
Thank you..,
-
Re: unable to print components in JPanel
You're making the same mistake that I pointed out earlier. Again the tutorials tell you not to use paint(...). Please re-read to see what they recommend.
- 06-04-2012, 09:18 AM #9
Senior Member
- Join Date
- Jan 2012
- Location
- TamilNadu
- Posts
- 162
- Rep Power
- 2
Re: unable to print components in JPanel
Thank You sir!
I will try with print(...)
-
Re: unable to print components in JPanel
Please look again at the tutorial link I've given you above, especially the last two lines where it talks about the printAll(Graphics g) method.
Similar Threads
-
Unable to load an image on JPanel
By shanthi in forum Java AppletsReplies: 1Last Post: 02-28-2011, 04:55 PM -
Unable to Print more than 1 File
By satyam000 in forum NetworkingReplies: 1Last Post: 11-24-2009, 07:25 AM -
Unable to Print more than 1 File
By satyam000 in forum AWT / SwingReplies: 5Last Post: 11-24-2009, 05:59 AM -
How to print Swing components
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
Removing components from JPanel
By Echilon in forum New To JavaReplies: 0Last Post: 12-30-2007, 04:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks