Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Swing / AWT / SWT
Print Text
Marcus Senna
Greenhorn
Posts: 21
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am trying to print the text in a JTextArea, which is in a JScrollPane. Right now i am only able to print what is viewable in the JScrollPane. I would like to print all of the text in the JTextArea. Below is the code i have been using. Thanks!!
public class SimulationResults extends JFrame implements ActionListener, Printable { ResultText newContentPane; JButton print; public SimulationResults() { this.setTitle("Simulation Results"); this.setSize(700, 400); Dimension frameSize = this.getSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int centerX = screenSize.width / 2; int centerY = screenSize.height / 2; int halfWidth = frameSize.width / 2; int halfHeight = frameSize.height / 2; this.setLocation(centerX - halfWidth, centerY - halfHeight); this.getContentPane().setLayout(new BorderLayout()); this.setBackground(Color.WHITE); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); ; print = new JButton("Print"); print.addActionListener(this); newContentPane = new ResultText(); newContentPane.setOpaque(true); this.getContentPane().add(newContentPane, BorderLayout.CENTER); this.getContentPane().add(print, BorderLayout.SOUTH); } public ResultText getNewContentPane() { return newContentPane; } public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D) g; g2d.translate(pf.getImageableX(), pf.getImageableY()); this.getContentPane().getComponent(0).print(g); return PAGE_EXISTS; } public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { job.print(); } catch (PrinterException ex) { } } } public class ResultText extends JPanel { JTextArea textArea; JScrollPane scrollPane; private final static String newline = "\n"; public ResultText() { super(new GridBagLayout()); textArea = new JTextArea(5, 20); textArea.setEditable(false); scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.weighty = 1.0; add(scrollPane, c); //textArea.removeAll(); } public void writeTextArea(String string) { textArea.append(string + newline + newline); textArea.setCaretPosition(textArea.getDocument().getLength()); } } }
Muhammad Saifuddin
Ranch Hand
Posts: 1325
I like...
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Is this possible to print the value inside JTextArea without using setText() or writeTextArea method??
Saifuddin..
[Blog]
[Linkedin]
How To Ask Questions On JavaRanch
My OpenSource
Marcus Senna
Greenhorn
Posts: 21
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The writeTextArea method is called by an object of my system, to display the status of that object on the JTextArea.
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Java print!
JTextPane printing issue
ScrollBar Problem
How to copy from 4 JTextFields to 1 JTextArea
How to print something that is not set initially
More...