472,804 Members | 969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,804 software developers and data experts.

Applet and Java Printing API

I am trying to print from an applet using the Java Print API.

If I run the applet on appletviewer then things work fine i.e. I am
able to get the print out. If I run the applet on a browser then the
print icon is loaded on the system tray and there is no print outs. I
am trying to print on HP laser jet printer.

Please help me to sort the err in the code or my understaning.

Thanks a ton!!!

JAVA_HelloWorld
Jul 17 '05 #1
5 29365

"java_helloworld" <ja*************@indiatimes.com> wrote in message
news:be**************************@posting.google.c om...
I am trying to print from an applet using the Java Print API.

If I run the applet on appletviewer then things work fine i.e. I am
able to get the print out. If I run the applet on a browser then the
print icon is loaded on the system tray and there is no print outs. I
am trying to print on HP laser jet printer.

Please help me to sort the err in the code or my understaning.

Thanks a ton!!!

JAVA_HelloWorld


Is the applet signed? If it's not, you won't be able to print due to
security constraints.
Jul 17 '05 #2
> Is the applet signed? If it's not, you won't be able to print due to
security constraints.


Yes the applet is not a signed Applet. I think with the Java printng
API we can print from an unsigned applet also. It just that before
printing it asks for a confirmation something like "Do you want to
allow the applet wants to print?".

I am able to instantiate the print icon on the system tray but not
able to spool to the printer.

If I run the code as a applet application i.e. using appletviewer then
things are fine but as I run on an HTML page the print icon loads on
the system tray but the print outs are not there on the printer.

Please excuse the coding standards.

Thanks,
Java Hello World

Here is the snapshot of the code.

package Applet.unsigned;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;

import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.swing.*;

public class PrinterApplet extends JApplet {
//~ Instance fields
================================================== ======

private PrintRequestAttributeSet aset;
DocFlavor DocFlavor;
public Doc doc;
DocPrintJob printerJob;
JTextPane pane;

//~ Methods ================================================== ==============

/**
* DOCUMENT ME!
*/
public void init() {
getContentPane().setLayout(new BorderLayout());

pane = new JTextPane();
pane.setSize(150, 100);
pane.setContentType("text/html");
pane.setText(
"<html><center><b><big>Applet Test</big></b><br>"
+ "</center></html>");
getContentPane().add(pane, "Center");

JPanel buttons = new JPanel();
buttons.setBackground(Color.white);

JButton print = new JButton("Print");

buttons.add(print);

getContentPane().add(buttons, "South");

print.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
print();
}
});
}

/**
* DOCUMENT ME!
*/
void prep() {

URL url = null;
aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));

try {
url =
new URL("http://sohowww.nascom.nasa.gov//data//realtime//eit_171//512//latest.gif");

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

PrintService pservices =
PrintServiceLookup.lookupDefaultPrintService();
System.out.println(pservices.getName());
//DocFlavor flavor = javax.print.DocFlavor.INPUT_STREAM.GIF;
DocFlavor flavor = javax.print.DocFlavor.URL.GIF;
//doc = new InputStreamDoc("a.gif", flavor);
//doc = new InputStreamDoc(pane.getText(), flavor);
doc = new SimpleDoc(url, javax.print.DocFlavor.URL.GIF, null);
try {
System.out.println("DOC : \n " + doc.getPrintData());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Create a Print Job */
printerJob = pservices.createPrintJob();

}

/**
* DOCUMENT ME!
*/
void print() {
prep();
System.out.println("Printer Name : " +
printerJob.getPrintService());
try {
printerJob.print(doc, aset);

} catch (PrintException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Done Printing.");
}

}
Jul 17 '05 #3
> url =
new URL("http://sohowww.nascom.nasa.gov//data//realtime//eit_171//512//latest.gif");


Here's your problem. An unsigned applet is not allowed to make a
connection to a host other than the one it was loaded from. I would
have thought DocPrintJob would throw an exception, but it appears to
be swallowing it silently.

url.openConnection();
url.openStream();

This will throw an AccessControlException as expected. Either sign the
applet, or host the image locally.
Jul 17 '05 #4
Thanks Murray, for the clarification. But I am able to print the
picture from the URL, if I run the Unsigned Applet as a Application
i.e. not on a web -browser.

BTW do you know any website/article/book, which talks about uses and
limitations of Java Printing and Unsigned/Signed Applet.
Jul 17 '05 #5
On 12 May 2004 03:15:16 -0700, java_helloworld wrote:

[ F'Ups set to c.l.j.help ]
Thanks Murray, for the clarification. But I am able to print the
picture from the URL, if I run the Unsigned Applet as a Application
i.e. not on a web -browser.


Yes, that supports what Murray was saying,
applications can do anything, whereas applets
are limited by the applet 'security sandbox'..

To 'break out' of the applet sandbox, you
need to jar and sign your classes..
<http://www.physci.org/codes/javafaq.jsp#jar>

[ Unfortunatley that entry focuses on 'double
click lauch' applications, but the 'jar/sign'
bit relates equally to applet security. ]

The only other option is to change the policy
files settings in the user's browser, which
seems only good for testing purposes..

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: nathanlaan | last post by:
This is the stupidest thing I have ever seen. Java 1.2, 1.3, and 1.4.1, and 1.4.2 all define the Applet.getDocumentBase() method differently! How am I supposed to get the directory of the document...
2
by: Michael | last post by:
Hello Currently I'm migrating an applet from Java 1.1 to 1.3. Targetplattform: Microsoft Internet Explorer 5.x with Sun's Java VM 1.3.1 Problem: The attached applet works fine with the...
0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
1
by: Tormod Omholt-Jensen | last post by:
need to dynamically insert an applet into a document. In IE 6.0 my code works fine, but there seems to be problems in Opera 7. The page looks like there is allocated space for an applet. ...
3
by: Larry Martin | last post by:
I am trying to run a Java Applet on my ascx page and am getting an IO exception when IE6 tries to load the applet. It seems a lot of others are getting the same problem but a search of the web did...
4
by: KenH | last post by:
I have a charting applet that has a print method that works just fine. My problem is I am trying to initiate the print method from a javascript routine but it doesn't work. I access my applet...
0
by: ankur | last post by:
WHEN I RUN THIS WEB APPLICATION ON Tomcat5.5.9 SERVER MY HttpChatApplet sccessfully Loaded from ChatDispatch but running on some another PC HttpChatApplet not loaded my Coad ...
0
by: shanmukhi | last post by:
Hi All I got a problem in running java programs. i am not able to run java applet while running i got a problem as Loading Java Applet Failed java.lang.NoClassDefFoundError: App (wrong...
0
by: shanmukhi | last post by:
Hi All I got a problem in running java Applet i am not able to run java applet while running i got a problem as Loading Java Applet Failed java.lang.NoClassDefFoundError: App (wrong name:...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.