How do I open up a print dialog box where you select your printer, page details, etc to print A SPECIFIED DOCUMENT OR JTEXTPANE?
Please help!
note: getDesktop().print gives me an error about printer setup, how to open the native print dialog?
Check out Lesson: Printing in Java tutorials, Using Print Setup Dialogs in particular. PrinterJob.printDialog() should do the trick.
An application displays a print dialog when the user presses a button related to the print command, or chooses an item from the print menu. To display this dialog, call the printDialog method of the PrinterJob class:
PrinterJob pj = PrinterJob.getPrinterJob();
//...
if (pj.printDialog()) {
try {pj.print();}
catch (PrinterException exc) {
System.out.println(exc);
}
}
//...
Reference:
Using Print Setup Dialogs
Lesson: Printing
Related
I have this idea, that anytime an unhandled exception occurs in my JavaFX program, that instead of relying on console output, I can display an alert to the user. I am thinking that perhaps I can capture the output from System.err to use. Here is what I have tried thus far.
PrintStream myStream = new PrintStream(System.err) {
#Override
public void println(String s) {
super.println(s);
Log.debugLog(s); //this function logs to a file and displays an alert to user
}
};
System.setErr(myStream);
This code segment works if I replace System.err with System.out, and System.setErr to System.setOut. However that is capturing System.out, not System.err. I suppose the better question would be, what exact function does System.err call when displaying an error to the console? So that may override it. Any tips are appreciated.
I think you have the wrong approach. If you want to display an alert to the user when there is an unhandled Exception, you can do that by setting a DefaultUncaughtExceptionHandler:
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
// show alert to user
e.printStackTrace();
// do whatever you want with the Exception e
});
You can create a JavaFX Alert customized to show an exception stack trace along with a brief message. This link shows the code for the below.
I'm working on some invoice software. So far, I managed to create an excel file (XLSX) with all the info I need (customer info, VAT, pricing etc.)
Now, I want to save this file to PDF so that it can be mailed directly to the customer. Seems kinda hard in Java. To make it easier, I just want to print my source file using the Windows Printing Dialog, and then select a PDF printer.
This little piece of code works, but it starts the printing job immediately using the default printer, without showing any dialog whatsoever. Not what I want.
desktop.print(new File("Docfile.pdf"));
This piece of code displays the printing dialog, but it's not clear to me (looking at the documentation) how I can tell a PrintJob to print a File or FileInputStream...
PrinterJob pj = PrinterJob.getPrinterJob();
pj.print();
Either the first code should display the dialog box, or the second one should give me the ability to select a file. Can't seem to fix it. Anyone got any ideas?
Start by taking a look at the Printing Trail and in particular Using Print Setup Dialogs
From the linked tutorials....
PrinterJob pj = PrinterJob.getPrinterJob();
...
if (pj.printDialog()) {
try {pj.print();}
catch (PrinterException exc) {
System.out.println(exc);
}
}
...
Is it possible to check whether the File Download Dialog Box in IE is visible using Selenium WebDriver or any other Java Library?
I don't want to download the file. I'm unable to verify the visibility of the dialog box.
Also I'm unable to close the dialog box.
Any help?
Check if your IE UNEXPECTED_ALERT_BEHAVIOR is set to IGNORE
try{
//Click the link that brings up the download dilaog
// Perform your next step : This is where the script will throw the excpetion due to the modal dialog.
}
catch(Exception e){
if(e.getMessage().contains("Modal dialog present")) {
System.out.println("A modal dialog is present. (which can be a download dialog)" );
System.out.println(e.getMessage());
// the exception message includes text contained in the dialaog. You can use it for validaton.
//Use a java robot class to cancel the dialog
Robot robot= new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
//Since the current focus is on cancel.. else use robot.keyPress(KeyEvent.VK_TAB) as needed.
}
I have to print a word document from java. I could just open it and print it. But the following code print it automatically. Is there any way to pop up a print dialogue to choose printer? If the user does not wish to print it, he should be possible to cancel it. Also I need to close the word after printing. Please help me.
public static void main(String args[]) throws IOException {
Desktop desktop = Desktop.getDesktop();
File f = new File("C:\\Users\\asa\\Desktop\\resume.doc");
desktop.open(f);
Thread.sleep(5000);
desktop.print(f);
}
Have you tried using Desktop#print(File file) method?
Try the Java print service API.
Tutorial here:
http://docs.oracle.com/javase/tutorial/2d/printing/services.html
I have the following code that shows a native print dialog in my java application.
PrinterJob job = PrinterJob.getPrinterJob();
PrintRequestAttributeSet atts = new HashPrintRequestAttributeSet();
atts.add(DialogTypeSelection.NATIVE);
if (job.printDialog(atts))
{
PrintService newlyUsedService = job.getPrintService();
DocPrintJob docJob = newlyUsedService.createPrintJob();
// print the passed javax.print.SimpleDoc object.
docJob.print(simpleDoc, atts);
}
This code works fine when a printer is selected and the Print button is clicked, but it does not work when the user selects any option from the “PDF” menu at the bottom left. The getPrintService method returns the last selected printer (Lillith from the screenshot) when one of the options in the "PDF" menu are selected.
Could anyone suggest what should be done to detect when “Save as PDF…” was selected in the print dialog and respond accordingly.
Have you tried adding a Print Job Listener? Just read up on it now, I hope it will be of some help:
Print Job Listener