Why the same font in Java looks different?

Why the same font looks different in Java app run from Netbeans and run from Jar?

  • In my Java Swing app I have some JTextAreas, I realize the text looks different when it's run from Netbeans and when the app is run from a Jar file, why ? How to make them look the same ? JTextArea Client_Side_TextArea=new JTextArea(),Network_TextArea=new JTextArea(); setLayout(new BorderLayout()); Client_Side_TextArea.setFont(new Font("Monospaced",0,15)); Client_Side_TextArea.setForeground(new Color(0,28,218)); Client_Side_TextArea.setPreferredSize(new Dimension(290,300)); Client_Side_TextArea.append(" Client Side\n================================\n"); add("West",Client_Side_TextArea); Network_TextArea.setFont(new Font("Monospaced",0,15)); Network_TextArea.setBackground(new Color(226,226,226)); Network_TextArea.setForeground(new Color(0,28,218)); Network_TextArea.setPreferredSize(new Dimension(270,300)); Network_TextArea.append(" Network Connection\n =========================================\n"); add("Center",Network_TextArea); In the following image, the upper part is from the app run with Netbeans, the lower part is how it looks when run from a Jar file :

  • Answer:

    This is the expected behavior: as discussed in the https://docs.oracle.com/javase/8/docs/api/java/awt/Font.html API, each supported platform may map a different physical font to a particulate logical font such as Font.MONOSPACED. Each https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html may further refine the choice of font for a particular purpose. Unless the platforms, versions and settings are identical, the fonts may vary. A complete example and more on the mapping may be found http://stackoverflow.com/a/26090878/230513. In addition, for the reasons cited http://stackoverflow.com/q/7229226/230513, don't use setPreferredSize(). If you choose to override getPreferredSize(), be certain not to fall into this http://stackoverflow.com/a/12532237/230513.

Frank at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.