Results 1 to 15 of 15
  1. #1
    arubin is offline Member
    Join Date
    Jul 2007
    Location
    Edgware, Middx UK
    Posts
    12
    Rep Power
    0

    Default PrintDialog save directory

    Hello,

    I have an application in which I often print to file from the PrintDialog. The default directory for saving the file is always the directory that program starts in. It is a pain having to change the save directory using the dialog. I want to be able to set this directory in the program. Is there any way to do this? I know how to set the directory in FileChooser but I need a way to get to PrintDialog's FileChooser.

    Thanks

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: PrintDialog save directory

    There is no PrintDialog class in the Java API. There is a printDialog() method for PrinterJob though. Can you please post the code you are using (to print the file) to help clarify the situation.

    Regards,
    Jim
    Last edited by jim829; 10-10-2017 at 03:22 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,965
    Rep Power
    7

    Default Re: PrintDialog save directory

    If you look at the source of RasterPrintJob and PSPrintJob, they do somethiing with the Destination class. You can try setting that in the PrintRequestAttributeSet. Maybe this code works for you:
    Java Code:
            PrinterJob job = PrinterJob.getPrinterJob();
            PageFormat pageFormat = job.defaultPage();
            PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
            set.add(new Destination(new File("/home/user/").toURI()));
            boolean b = job.printDialog(set);
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: PrintDialog save directory

    I actually recommended that at first but then scrubbed my reply because I searched and found a PrintDialog class ... in C#. Hence my current post, just to ensure we *are* talking about Java.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    arubin is offline Member
    Join Date
    Jul 2007
    Location
    Edgware, Middx UK
    Posts
    12
    Rep Power
    0

    Default Re: PrintDialog save directory

    This is the code. I need the alternatives of printing or saving to file.
    Java Code:
    if (source == printButton) {
    	        try {
    	    PrinterJob printJob = PrinterJob.getPrinterJob();
    	    if (printJob.printDialog()) {
    	          Paper paper = new Paper();
    	          paper.setSize(589,834);
    	          paper.setImageableArea(18,18,554,802);
    	          PageFormat p = new PageFormat();
    	          p.setPaper(paper);
    	          printJob.setPrintable(this, p);
    	          printJob.print();
    
    
    	        }} catch (Exception ex) {
    	            ex.printStackTrace();
    	    }
    Last edited by arubin; 10-10-2017 at 03:39 PM.

  6. #6
    arubin is offline Member
    Join Date
    Jul 2007
    Location
    Edgware, Middx UK
    Posts
    12
    Rep Power
    0

    Default Re: PrintDialog save directory

    That looks promising. I'll try it tonight.

    Quote Originally Posted by SurfMan View Post
    If you look at the source of RasterPrintJob and PSPrintJob, they do somethiing with the Destination class. You can try setting that in the PrintRequestAttributeSet. Maybe this code works for you:
    Java Code:
            PrinterJob job = PrinterJob.getPrinterJob();
            PageFormat pageFormat = job.defaultPage();
            PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
            set.add(new Destination(new File("/home/user/").toURI()));
            boolean b = job.printDialog(set);

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: PrintDialog save directory

    Ok, but how are you printing to a file (as you stated in your inital post)? It appears that you are printing to the printer in that example.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    arubin is offline Member
    Join Date
    Jul 2007
    Location
    Edgware, Middx UK
    Posts
    12
    Rep Power
    0

    Default Re: PrintDialog save directory

    I have the choice from the printDialog. Either print to a file or save as a .ps file.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: PrintDialog save directory

    Ok, I just tried it and SurfMan's suggestion worked for me with one caveat. In my case, I had to give it the complete destination file and not just the directory. So make certain you tag on a default file name too (which is easy to edit once the dialog appears).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    arubin is offline Member
    Join Date
    Jul 2007
    Location
    Edgware, Middx UK
    Posts
    12
    Rep Power
    0

    Default Re: PrintDialog save directory

    Well that works but it is only half the job.
    In fact when I got home I found something similar elsewhere in my code where I print a list of documents without going through the printDialog() and I had the code

    Java Code:
    aset.add(new Destination(new java.net.URI("file:/home/alan/statement"+ s1+ dateText+ ".ps")));
    I hadn't thought to use that with printDialog()

    I say it is only half the job because I also want to retrieve the file path if I navigate to another folder. This is similar to what one would use with JFileCooser
    Java Code:
    String s = fileChooser.getSelectedFile().getAbsolutePath()

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: PrintDialog save directory

    Still not certain what you want. My assumption is you want to navigate to a particular directory and store the file there. If so, why not use JFileChooser to select the directory, append a default file name to the retrieved path, and set the destination attribute.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    arubin is offline Member
    Join Date
    Jul 2007
    Location
    Edgware, Middx UK
    Posts
    12
    Rep Power
    0

    Default Re: PrintDialog save directory

    I think I have solved this.

    Java Code:
    printJob.print(set);
    string = set.get(Destination.class).toString();
    gives me just what I need.

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: PrintDialog save directory

    Well, I'm confused since it appears you are just printing out the path name that you just set via the Destination attribute class. Also, if your attribute set does not contain a Destination attribute, you will throw a null pointer exception.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    arubin is offline Member
    Join Date
    Jul 2007
    Location
    Edgware, Middx UK
    Posts
    12
    Rep Power
    0

    Default Re: PrintDialog save directory

    It is not necessarily the same path because if I have navigated to a different directory when I printed to file from the print dialog the Destination attribute will now contain that file path. What I now have is

    Java Code:
    PageFormat p = printJob.defaultPage();
    PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
    String s1 = house.keyref.replace('/', '_');
    set.add(new Destination(new java.net.URI(constants.getPsURL()+ "statement"+ s1+ dateText+ ".ps")));
    if (printJob.printDialog(set)) {
         Paper paper = new Paper();
         paper.setSize(589,834);
         paper.setImageableArea(18,18,554,802);
         p.setPaper(paper);
         printJob.setPrintable(this, p);
         printJob.print(set);
         if(set.get(Destination.class)!=null){
             String ss = set.get(Destination.class).toString();
             constants.setPsURL(ss.substring(0,ss.lastIndexOf('/')+1));       	  
              }
    I want to save a statement to a ps file. The print dialog opens using the file path stored at constants.psURL. After the printjob is done the file path that the ps file was actually written to is saved at constants.psURL. Next time I want to save a ps file the file path will be the last actual file path that was used rather than the program default.

    If, instead of saving a ps file I decided to send to the printer then, as you say, set.get(Destination.class) will be null.

  15. #15
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: PrintDialog save directory

    Got it! Glad it solves your problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 7
    Last Post: 04-30-2014, 12:43 PM
  2. Need help with java save directory
    By anuraagkb in forum New To Java
    Replies: 1
    Last Post: 01-29-2012, 04:11 PM
  3. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 08:31 PM
  4. JDK Directory
    By PhQ in forum Eclipse
    Replies: 1
    Last Post: 03-21-2010, 03:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •