Results 1 to 2 of 2
  1. #1
    vipintembhare is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Angry Problem with NetBeans printing code

    I got some problem when using that netbeans Jframe Printing code for ma project,it doesn't prints when i call it from other jframe.....

    here's ma calling code

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /*
    * Accounting.java
    *
    * Created on Mar 14, 2012, 11:38:12 PM
    */
    package pathology;
    import java.sql.*;
    import javax.swing.JOptionPane;


    /**
    *
    * @author KUNDNA
    */
    public class Accounting extends javax.swing.JFrame {
    Connection con;
    Statement st;
    ResultSet rs;
    public static String d1,d2;
    /** Creates new form Accounting */
    public Accounting() {
    initComponents();
    }


    /** 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();
    jPanel1 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jPanel2 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
    jLabel1.setText("Total Collection Report");

    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel2.setText("From Date");

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel3.setText("To Date");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(26, 26, 26)
    .addComponent(jLabel2)
    .addGap(18, 18, 18)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jLabel3)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(122, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(28, 28, 28)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel2)
    .addComponent(jLabel3)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(46, Short.MAX_VALUE))
    );

    jButton1.setFont(new java.awt.Font("Tahoma", 1, 14));
    jButton1.setText("Print");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jButton3.setFont(new java.awt.Font("Tahoma", 1, 14));
    jButton3.setText("Cancel");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton3ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(111, 111, 111)
    .addComponent(jButton1)
    .addGap(52, 52, 52)
    .addComponent(jButton3)
    .addContainerGap(161, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(23, 23, 23)
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(95, 95, 95)
    .addComponent(jLabel1)
    .addContainerGap(96, Short.MAX_VALUE))
    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(23, 23, 23)
    .addComponent(jLabel1)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(44, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    this.setVisible(false);
    new Mainform().setVisible(true);// TODO add your handling code here:
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    d1=jTextField1.getText();
    d2=jTextField2.getText();
    this.setVisible(false);
    new Account().setVisible(true);
    // TODO add your handling code here:
    }

    /**
    * @param args the command line arguments
    */
    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 How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(Accounting.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(Accounting.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(Accounting.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(Accounting.clas s.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 Accounting().setVisible(true);
    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration
    }



    and Actual code for printing as per ur suggested method....


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /*
    * Account.java
    *
    * Created on May 9, 2012, 1:37:22 PM
    */
    package pathology;

    import java.sql.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionListener;
    import java.awt.print.*;

    import javax.swing.JOptionPane;
    public class Account extends javax.swing.JFrame implements Printable{
    JFrame frameToPrint;



    Object rowData[][] = {{"Row1-Column1", "Row1-Column2", "Row1-Column3", "Row1-Column4","hey -now","kyun","erwre","erwerwererw","ewjane "}};
    Object columnNames[] = {"Patient_ID","Mr/Mrs/Miss","Patient_Name","Amount"};
    final DefaultTableModel mTableModel = new DefaultTableModel(rowData, columnNames);
    Connection con=null;
    ResultSet rs=null;
    Statement st=null;
    int amount=0;
    static JFrame B = new Account();
    /** Creates new form Account */
    public Account() {
    connect();

    initComponents();
    jLabel2.setText(Accounting.d1);
    jLabel3.setText(Accounting.d2);
    try
    {
    rs=st.executeQuery("select * from Patient2 where Date1 between '"+Accounting.d1+"' and '"+Accounting.d2+"' ");
    while (mTableModel.getRowCount() > 0) {
    mTableModel.removeRow(0);
    }
    Object[] rows1;
    int sum=0;

    while (rs.next()) {



    rows1 = new Object[]{ rs.getString(1),rs.getString(2),rs.getString(3),rs .getString(10)};
    //int b=rs.getInt(10);
    //sum=sum +b;
    // add the temp row to the table
    mTableModel.addRow(rows1);

    }
    //int row=jTable1.getSelectedColumn();
    for(int i=0;i<jTable1.getRowCount();i++)
    {
    String s=jTable1.getModel().getValueAt(i, 3).toString();
    int a=Integer.parseInt(s);
    amount=amount+a;
    }
    jLabel6.setText(amount +"");
    }
    catch(Exception e){
    JOptionPane.showMessageDialog(this, e);
    }
    }

    public Account(JFrame f){
    frameToPrint=f;
    }
    public void connect()
    {
    try{
    String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
    Class.forName(Driver);


    String db="jdbc:odbc:Database";
    con=DriverManager.getConnection(db);
    st=con.createStatement();
    }
    catch(Exception ex)
    {
    JOptionPane.showMessageDialog(null, ex);
    }
    }
    /** 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();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable(mTableModel);

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
    jLabel1.setText("Accounting for");

    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));

    jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel4.setText("to");

    jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18));
    jLabel5.setText("Amount");

    jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14));

    jButton1.setText("Print");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jButton2.setText("Cancel");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton2ActionPerformed(evt);
    }
    });

    jTable1.setModel(mTableModel);
    jScrollPane1.setViewportView(jTable1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(243, 243, 243)
    .addComponent(jButton1)
    .addGap(29, 29, 29)
    .addComponent(jButton2))
    .addGroup(layout.createSequentialGroup()
    .addGap(329, 329, 329)
    .addComponent(jLabel5)
    .addGap(18, 18, 18)
    .addComponent(jLabel6))
    .addGroup(layout.createSequentialGroup()
    .addGap(105, 105, 105)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel1)
    .addGap(28, 28, 28)
    .addComponent(jLabel2)
    .addGap(26, 26, 26)
    .addComponent(jLabel4)
    .addGap(29, 29, 29)
    .addComponent(jLabel3))
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE))))
    .addContainerGap(149, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(42, 42, 42)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jLabel1)
    .addComponent(jLabel2)
    .addComponent(jLabel4)
    .addComponent(jLabel3))
    .addGap(58, 58, 58)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jLabel6)
    .addComponent(jLabel5))
    .addGap(87, 87, 87)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jButton1)
    .addComponent(jButton2))
    .addGap(25, 25, 25))
    );

    pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jButton1.setVisible(false);
    jButton2.setVisible(false);// TODO add your handling code here:
    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());
    }
    }// TODO add your handling code here:
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    this.setVisible(false);
    new Accounting().setVisible(true);// TODO add your handling code here:
    }
    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);
    B.print(g);
    // Turn double buffering back on
    return(PAGE_EXISTS);
    }

    }
    /**
    * @param args the command line arguments
    */
    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 How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(Account.class.g etName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(Account.class.g etName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(Account.class.g etName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(Account.class.g etName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    B.setVisible(true);
    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration

    }

    please help me out...........desperately needed....please

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Problem with NetBeans printing code

    Removed from Print jFrame . When you have a question, start your own thread - they're free. Don't post to an old dead thread and don't hijack another poster's thread.

    Please go through the following:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Very few, if any, volunteers on a forum are prepared to go through hundreds of lines of code.

    Remove useless comments before posting on a forum. They only add clutter.

    The NetBeans visual designer isn't a beginners' tool. Stay away from it unless you intend to make a living designing Swing GUIs. Hand coding GUIs isn't difficult, and you are a few orders of magnitude more likely to get help if you have a problem. Auto-generated code isn't designed to be read, so nobody will even try.

    Finally, your desperation is of no interest to other members here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 02-10-2012, 09:50 AM
  2. Replies: 6
    Last Post: 01-19-2012, 05:16 PM
  3. How do I run this code in Netbeans?
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 07-28-2011, 07:47 AM
  4. Replies: 8
    Last Post: 06-06-2011, 02:27 AM
  5. Adding printing in Netbeans app
    By aryan.838 in forum AWT / Swing
    Replies: 4
    Last Post: 03-31-2010, 03:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •