Printing Jasper Report to connected printer device in JAVA


Printing Report is one of the major task in all the application.Its the main documenting purpose too.During printing we have to consider one major thing that what are the printers that have been currently connected to the system in order to print those document.Following code may help you to analyse that what are the component that have been currently configured to the system
// Comment
private void printaction() 
    {   Object[] obj = {"Choose the Printer \n\n", jcmb_selectprinter};
        Object stringArray[] = {"Print","Cancel"};
         if (JOptionPane.showOptionDialog(null, obj, "Choose the Printer",
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, stringArray, obj) == JOptionPane.YES_OPTION)
         {
         String printerne= jcmb_selectprinter.getSelectedItem().toString();
         try
            {

            Connection conn = null;
            HashMap params = new HashMap();
         
      try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDB","root","root");
        } 
        catch (SQLException ex) 
        {
            JOptionPane.showMessageDialog(null, ex);
        } 
        catch (ClassNotFoundException ex) 
        {
            JOptionPane.showMessageDialog(null, ex);
        }

            JasperReport report = (JasperReport)
            JRLoader.loadObject(this.getClass().getResource("/report/yourReport.jasper"));
            JasperPrint print = JasperFillManager.fillReport(report, params, conn);
            print.setOrientation(JasperReport.ORIENTATION_PORTRAIT);
            PrinterJob job = PrinterJob.getPrinterJob();
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            int selectedService = 0;
         
            for(int i = 0; i < services.length;i++)
            {
              
            if(services[i].getName().toUpperCase().equals(printerne.toUpperCase()))
            {
            selectedService = i;
            }
            }
            job.setPrintService(services[selectedService]);
            PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
            MediaSizeName mediaSizeName = MediaSize.findMedia( Float.parseFloat( String.valueOf(8.264)), Float.parseFloat( String.valueOf(11.694)),MediaPrintableArea.INCH);
            printRequestAttributeSet.add(mediaSizeName);
            printRequestAttributeSet.add(new Copies(1));
            JRPrintServiceExporter exporter;
            exporter = new JRPrintServiceExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]);
            exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[selectedService].getAttributes());
            exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
            exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
            exporter.exportReport();
        cn.conn.close();
          JOptionPane.showMessageDialog(null,"printed");
            }

        catch (JRException ex)
            {
             
            }
            catch (SQLException ex)
        {
            JOptionPane.showMessageDialog(null,ex);
        }
         catch (PrinterException ex) {
            
            }
              }
        else
         {
            JOptionPane.showMessageDialog(null," canceled your process");
         }
    }

    } 
The above code help us print the selected document directly to the printer.Here in this sample code we have used the jasper report to print the document straightly to print the document.
The following code helps you load the number of printers connected to your system in a jComboBox.


// Comment

       
public void printer()
    {  jcmb_selectprinter.removeAllItems();
        PrintService[] ps = PrintServiceLookup.lookupPrintServices(null, null);
        PrintService[] psdef = PrintServiceLookup.lookupPrintServices(null, null);
        for(int i=0;i<ps.length;i++)
        {
            if(ps[i].getName().equals(psdef[i].getName()))
            {jcmb_selectprinter.addItem(psdef[i].getName());
              
            } else {
                System.out.println(psdef[i].getName());

            }
        }
    }
 

raja P Web Developer

DVD Ejector in VB


Its a great time for a software programmers to control their system by them self.For doing this i step towards for developing the small software the DVD Ejector.Using this following piece of code you can create a software to eject your dvd or/cd drive to open and close.
// Comment
Public Class frmEject
    'you need window media player reference in your project
    '1 combo box
    ' 2 button control
    Dim omal As New WMPLib.WindowsMediaPlayer
    Dim total As Integer
    Dim i As Integer

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        omal.close()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        total = 0
        i = 0
        total = omal.cdromCollection.count
        If (total >= 1) Then
            For i = 0 To (total - 1)
                ComboBox1.Items.Add(omal.cdromCollection.Item(i).driveSpecifier)
            Next
            ComboBox1.Text = ComboBox1.Items.Item(0)
        End If
        MyBase.CenterToScreen()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not (ComboBox1.Text = "") Then
            omal.cdromCollection.Item(ComboBox1.SelectedIndex).eject()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        For i = 0 To (total - 1)
            omal.cdromCollection.Item(i).eject()
        Next
    End Sub
End Class


raja P Web Developer

Keyboard Event Handling Mechanism in java


Now a day key events plays a major role in the application developing which help data application enter makes more fast and they are able to type the key event handling mechanism easily using their application.

Following are sample code of key event handling.

// Comment
private void jKeyPressed(java.awt.event.KeyEvent evt) {   

int a=evt.getKeyCode();
        if(a==10) // Enter Key
        {
         jTextFilednext.requestFocus();
        }
        else if(a==38) // up Arrow Key
        {
           jTextFiledPrevious.requestFocus();
        }
        else if(a==40) // Down Arrow Key
        {
        jTextFilednext.requestFocus();
        }


   if(a==37) // up left Key
        {
             jTextFiledPrevious.requestFocus();
        } else if(a==39) // up right Key
        {
            jTextFiledCurrent.requestFocus();
        } else if(a==10) // up Entert Key
        {
             jTextFilednext.requestFocus();
        }
raja P Web Developer

CREATE A JAVA BEAN TO DRAW VARIOUS GRAPHICAL SHAPES WITH OR WITHOUT BDK



AIM:
     To create a java Bean to draw graphical shapes and display it using BDK       

PROCDURE:
Ø      Start the program.
Ø      Create a java file for drawing rectangle.
Ø      Create a manifeast.
Ø      Create jar file and load it in to the bean window.
Ø      Display the shape.
Ø      Terminate the program.

PROGRAM:

SimpleBean.java

// Comment
import java.awt.*;
import java.io.*;
public class SimpleBean extends Canvas implements Serializable
{
private Color myColor;
public SimpleBean()
{
myColor=Color.white;
setBackground (myColor);
}
public Color getmyColor() {return myColor;}
public void setmyColor(Color c){myColor=c;}
public void paint(Graphics g)
{
g.setColor(myColor);
g.fillRect(0,0,getSize().width,getSize().height);
}         
public Dimension getPreferredSize()
{
return new Dimension(50,50);}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
}

BeanTest.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
public class BeanTest extends Applet
{
SimpleBean bean;
public static void main(String args[])
{
Frame f=new Frame("BeanTest-SimpleBean");
BeanTest me=new BeanTest();
f.add(me,"Center");
me.init();
me.start();
f.pack ();
f.addWindowListener (new WindowAdapter ()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
f.setVisible (true);
}
public void init ()
{
SetBackground (Color.lightGray);
add (new Label("BeanTest-SimpleBean"));
try
{
bean= (SimpleBean)Beans. instantiate (null,"SimpleBean");
add(bean);
}
catch(Exception e)
{
add(new Label("cannot instantiate the Bean!"));
e.printStackTrace();
}
}
} 
SimpleBean.mf Name: SimpleBean.class Java-Bean: True

Screen Shot
raja P Web Developer

Related Posts Plugin for WordPress, Blogger...