Java Yazıcıdan Çıktı Alma (Java Printer Kullanımı)

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class YazicidanCiktiAl extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private JLabel lblDokuman;
private JLabel lblAdres;
private JComboBox cmbTip;
private JTextField txtAdres;
private JButton btnYazdir;

public YazicidanCiktiAl(){
lblDokuman=new JLabel("Döküman Tipi");
lblAdres=new JLabel("Adres");
cmbTip=new JComboBox();
cmbTip.addItem("GIF");
cmbTip.addItem("PDF");
cmbTip.addItem("PNG");
cmbTip.addItem("JPEG");
txtAdres=new JTextField();
btnYazdir=new JButton("Yazdır");
setTitle("E-Hero");
setSize(400,400);
setLocation(300,200);
pencereGoster();
}

public void pencereGoster(){
Container con=this.getContentPane();
con.setLayout(null);
lblDokuman.setBounds(30,30,100,25);
lblAdres.setBounds(30,70,100,25);
cmbTip.setBounds(120,30,200,25);
txtAdres.setBounds(120,70,200,25);
btnYazdir.setBounds(140,110,120,30);
con.add(lblDokuman);
con.add(lblAdres);
con.add(txtAdres);
con.add(btnYazdir);
con.add(cmbTip);
btnYazdir.addActionListener(this);

}

public static void main(String[] args) {
YazicidanCiktiAl yca=new YazicidanCiktiAl();
yca.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

DocFlavor flavor=null;

if(cmbTip.getSelectedItem().toString().equals("GIF"))
{
flavor = DocFlavor.INPUT_STREAM.GIF;
}

else if(cmbTip.getSelectedItem().toString().equals("PNG"))
{
flavor = DocFlavor.INPUT_STREAM.PNG;
}

else if(cmbTip.getSelectedItem().toString().equals("PDF"))
{
flavor = DocFlavor.INPUT_STREAM.PDF;
}

else if(cmbTip.getSelectedItem().toString().equals("JPEG"))
{
flavor = DocFlavor.INPUT_STREAM.JPEG;
}

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.NA_LETTER);
aset.add(new Copies(1));

PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(flavor, aset);

if (pservices.length > 0) {
DocPrintJob printJob = pservices[0].createPrintJob();

Doc doc = new InputStreamDoc(txtAdres.getText(), flavor);

try {

printJob.print(doc, aset);

} catch (PrintException ex) {

JOptionPane.showMessageDialog(null,"Döküman Yazdırılamadı");
}
} else {

JOptionPane.showMessageDialog(null,"Yazıcı Uygun Degil");
}
}
}

Proje İndir

« »