Solved

Java programming

Posted on 2013-11-11
3
400 Views
Last Modified: 2013-11-11
I am using the following code to print a string to my prnter.  It appears as though the font is proportional and makes it impossible to keep columns straight.  How would I set the font to something like times Roman so I can set up a formatted string that will not be all over the place

                JTextPane textPane = new JTextPane();

                textPane.setText(printString);
                try {    
                    textPane.print();
0
Comment
Question by:sargent240
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
After creating your JTextPane you just need to set the "font" on it. Add the following line just after you call the constructor...
textPane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));

Open in new window

By the way, Times Roman is NOT a fixed-width font, you would still get the same issues. In the above, feel free to replace the use of the Font.MONOSPACED constant with an actual font name if you want to tweak it further. Fonts like "Courier New" are the fixed-width fonts that you would be after. Using the constant however, should be more portable across different systems, although I doubt you could find a computer/os that doesn't have Courier New available!

Obviously, you can also play with the point size and style specified in the line above to further change the desired output.
0
 

Author Closing Comment

by:sargent240
Comment Utility
Thank you very much for your help. and the added explanation.
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Not a problem! :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now