Java: Printing program output to a physical printer

后端 未结 3 739
时光说笑
时光说笑 2020-12-09 12:21

I am a relatively new programmer so this may be a really simple question to answer but its got me a bit stumped..

I\'m trying to print my Java GUI\'s final output t

3条回答
  •  一整个雨季
    2020-12-09 13:13

    Create a JTextComponent (I suggest a JTextArea so you can use append()), and append what you need to the field. Do not display it on the view, it is simply a hidden field for printing purposes.

    All JTextComponents have a print() method. Simply call hiddenTextArea.print() and the rest is handled for you.

            JTextArea hiddenTextArea = new JTextArea();
            for (String s : dataToPrintCollection) {
                hiddenTextArea.append(s + "\n");
            }
    
            try {
                hiddenTextArea.print();
            } catch (PrinterException e) {}
    

提交回复
热议问题