简体   繁体   中英

How to enable landscape orientation in PrinterJob.pageDialog

I want to have option to set a page format in PrinterJob.pageDialog and the only missing is to change page orientation, because the radio button for landscape mode is disabled.

The is that I do not know how to enable this radio button representing landscape mode in this dialog. 是我不知道如何在此对话框中启用代表横向模式的单选按钮。 It is only enabled when the orientation mode is set to landscape before opening the dialog. This is not a good solution for me since I want always to show to the user his previous selection, when he opens the dialog again, while giving him option to change it.

I really would like to avoid building a dialog myself since this look perfect.

I heard that it might be an issue present only on Window machines. Please, let me know if it occurs also on other platforms.

Thanks in advance.



import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;

public class PrintDialogSSCCE extends JFrame
{
    private static final long serialVersionUID = 1L;
    private PageFormat pageFormat;

    public PrintDialogSSCCE()
    {
        super();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PrinterJob pj = PrinterJob.getPrinterJob();
        pageFormat = pj.defaultPage();
//      pageFormat.setOrientation(PageFormat.LANDSCAPE);
        JMenuBar mb = new JMenuBar();
        JMenu file = new JMenu("File", true);
        file.add(new FilePageSetupAction()).setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK
                | Event.SHIFT_MASK));
        mb.add(file);
        setJMenuBar(mb);
    }

    public class FilePageSetupAction extends AbstractAction
    {
        private static final long serialVersionUID = 1L;

        public FilePageSetupAction()
        {
            super("Page setup...");
        }
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            PrinterJob pj = PrinterJob.getPrinterJob();
            pageFormat = pj.pageDialog(pageFormat);
        }
    }

    public static void main(String[] args)
    {
        PrintDialogSSCCE pd = new PrintDialogSSCCE();
        pd.setSize(300, 300);
        pd.setVisible(true);
    }
}

Taking trashgod's advice I am posting my own findings as answer to this question so we can finish this one off.

OK. I got it sorted. Thanks karakuricoder. Your info lead me to the solution. The problem is present always when I have my default printer set to MS Office Image Writer. It is fine when I have any other selected. Now I wonder if other people have the same issue? And maybe an explanation why? Damn MS printer, I wasted good few hours digging after this problem. To be honest I still like this printer. :) I had it as default as recently I have been testing lots of Java API and it is useful to see how the documents look when printed to file. Thanks a lot.

Up. I see it depends on the printer settings. I played with them for a bit and I am still unable to set it to have both options for a page format that is set to portrait.

Only working solution is, not to have MS Printer set as the default one.

Good luck all, Boro.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM