Results 1 to 8 of 8
Thread: Print jFrame
- 03-30-2010, 07:38 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 67
- Rep Power
- 0
- 03-30-2010, 11:35 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I don't think there is a simple/direct way to do is.
What you can do is, take screen shot of your UI and print it. Read more about java.awt.print for printing a document.
- 03-30-2010, 05:10 PM #3
i've already seen code that can print components, for example here Swing Tutorial: High-Quality Java Printing but i never check it out. if you try it out could you please give a short information if it works?
- 03-31-2010, 03:11 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It use the java.awt.print. Nothing else. ;p
- 03-31-2010, 06:17 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 67
- Rep Power
- 0
hey guys... i could find a code which is printing a gui form with all components.. it's working fine..
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.print.*; class PrintUIWindow implements Printable, ActionListener { JFrame frameToPrint; public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()-55); frameToPrint.print(g); return PAGE_EXISTS; } public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { job.print(); } catch (PrinterException ex) { } } } public PrintUIWindow(JFrame f) { frameToPrint = f; } public static void main(String args[]) { UIManager.put("swing.boldMetal", Boolean.FALSE); JFrame f = new JFrame("Print UI Example"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); JLabel label1=new JLabel("Selling Bill",JLabel.CENTER); JLabel label2=new JLabel("Customer Name :Mahmoud Saleh ",JLabel.LEFT); JLabel label3=new JLabel("Buying Date :29/8/2008 ",JLabel.LEFT); JLabel label4=new JLabel("Book Buyed :Java Printing ",JLabel.LEFT); JLabel label5=new JLabel("Number : 6 Copies ",JLabel.LEFT); JLabel label6=new JLabel("Total Price :600 $ ",JLabel.LEFT); label1.setFont(new Font("Courier New", Font.BOLD, 13)); label2.setFont(new Font("Courier New", Font.BOLD, 13)); label3.setFont(new Font("Courier New", Font.BOLD, 13)); label4.setFont(new Font("Courier New", Font.BOLD, 13)); label5.setFont(new Font("Courier New", Font.BOLD, 13)); label6.setFont(new Font("Courier New", Font.BOLD, 13)); JButton printButton = new JButton("Print This Window"); printButton.addActionListener(new PrintUIWindow(f)); JPanel panel=new JPanel(); panel.setLayout(new GridLayout(6,1)); panel.add(label1); panel.add(label2); panel.add(label3); panel.add(label4); panel.add(label5); panel.add(label6); f.setSize(300,300); f.setLocationRelativeTo(null); f.add(panel,BorderLayout.CENTER); f.add(printButton,BorderLayout.SOUTH); panel.setBackground(Color.WHITE); f.setResizable(false); f.setVisible(true); } } -------------------------------------------------------------------- and i also created a small frame using netbeans and it's also working.. hope this will help u guys too.... /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * SimplePrint.java * * Created on Jan 11, 2010, 4:29:16 PM */ package home.tess.ukw; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionListener; import java.awt.print.*; import javax.swing.JFrame; import javax.swing.JOptionPane; /** * * @author uthpalaw */ public class SimplePrint extends javax.swing.JFrame implements Printable { JFrame frameToPrint; static JFrame SP = new SimplePrint(); /** Creates new form SimplePrint */ public SimplePrint() { initComponents(); } public SimplePrint(JFrame f){ frameToPrint = f; } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Simple Print Form"); jLabel1.setText("Hello..."); jButton1.setText("Print"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addContainerGap(361, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(51, 51, 51) .addComponent(jLabel1) .addGap(65, 65, 65) .addComponent(jButton1) .addContainerGap(132, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: // JOptionPane.showMessageDialog(null, "hello"); PrinterJob PJ = PrinterJob.getPrinterJob(); //PageFormat PF = PJ.pageDialog(PJ.defaultPage()); PJ.setPrintable((Printable) this); boolean doPrint = PJ.printDialog(); //JOptionPane.showMessageDialog(null, doPrint); if(doPrint){ try { PJ.print(); } catch (PrinterException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } } public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return(NO_SUCH_PAGE); } else { Graphics2D g2d = (Graphics2D)g; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); // Turn off double buffering //componentToBePrinted.paint(g2d); //frameToPrint.print(g); SP.print(g); // Turn double buffering back on return(PAGE_EXISTS); } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { SP.setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; // End of variables declaration }Last edited by Eranga; 03-31-2010 at 01:36 PM. Reason: added code tags
- 03-31-2010, 01:35 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Fine, if you are satisfied with what you have mean you found the solution. If so please mark the thread solved. And also please don't forget to use code tags next time. Good luck! :)
- 04-01-2010, 05:41 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 67
- Rep Power
- 0
Could you please show me how to make this thread as solved. ?
- 04-02-2010, 05:39 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
On top of the thread, you can find a menu. Click on Thread Tools there, you can find the option to mark the thread solved.
Similar Threads
-
how to print
By anilkumar_vist in forum New To JavaReplies: 0Last Post: 03-18-2010, 02:23 PM -
Passing data from one JFrame to another JFrame
By tarami in forum New To JavaReplies: 3Last Post: 08-06-2009, 05:44 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
Print A Pdf
By Jack in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 05:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks