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
Ron McLeod
Paul Clapham
Tim Cooke
Devaka Cooray
Sheriffs:
Liutauras Vilda
paul wheaton
Rob Spoor
Saloon Keepers:
Tim Moores
Stephan van Hulst
Tim Holloway
Piet Souris
Mikalai Zaikin
Bartenders:
Carey Brown
Roland Mueller
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.
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
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...