print out command in applet
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Krupalsinh_Gohil
Posted On:   Friday, September 13, 2002 10:03 PM

can i put a print out button in appet through which i can get print out through printer of any records in applet

Re: print out command in applet

Posted By:   amit_raha  
Posted On:   Friday, September 13, 2002 11:08 PM

hi krupalsinh,


you can print from applet .. for that you have use AWT pakage and also printjob package.For getting a print you have to add one frame by performing some action on the applet and then you have to add one canvas into the frame which will hold printable items.


i am giving the code segments here .. just try this ..

import the pacages ---


import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.awt.PrintJob;

import java.util.Properties;



then where you want to add the frame ...
there you the segment of code as bellow---



Frame frame=new Frame("New Frame");

frame.add(canvas,BorderLayout.CENTER);

PrintCanvas canvas = new PrintCanvas();

button3.setLabel("Print");

button4.setLabel("Close");

panel4.add(button3);

panel4.add(button4);

frame.add(panel4,BorderLayout.SOUTH);

frame.setSize(330,300);

frame.setVisible(true);



and then in button3 actionperformed method .. you have to write like this ..



void button3_actionPerformed(ActionEvent e) {

textField1.setText("inside print from Applet frame");

Toolkit tk=frame.getToolkit();

Properties props=new Properties();

props.put("name", "amit");

PrintJob printJob=tk.getPrintJob(frame,"sam print",props);

if (printJob != null) {

Graphics pg = printJob.getGraphics();


if (pg != null) {

canvas.printAll(pg);

pg.dispose(); // flush page

}

printJob.end();


}


}



That will be enough to get a printout from a applet ..
just try this and have fun ..


regards

amit

About | Sitemap | Contact