0

I noticed the fonts look different when I run my Java Swing app from Netbeans and when I run it as a jar file from my C: drive on Windows 7.

I was told the fonts used in jars apps come from the system, so they look different from the fonts when run from Netbeans, so to keep my app look consistent from my IDE and from user perspective, how to locate the fonts being loaded into my app from Netbeans so I can copy them into my app and package them as part of my jar app, is this doable ? If so after I copy them into my app, how to load them inside my app ?

Here is the code and images :

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class Test_JPanel extends JPanel
{
  public static final long serialVersionUID=26362862L;
  static Dimension Screen_Size=Toolkit.getDefaultToolkit().getScreenSize();
  Thread Empty_JPanel_Thread;

  public Test_JPanel()
  {
    setPreferredSize(new Dimension(300,600));
    String Table="<Html>\n"+
                 "  <Center><Br>\n"
                 +"Test Table<P>\n"
                 +"    <Table Border=0 Cellpadding=1 Cellspacing=1 Width=286>\n"
                 +"      <Tr><Td Width=118 Align=Right>[ 1]&nbsp;&nbsp;</Td><Td>111 - AAA</Td><Td></Td></Tr>\n"
                 +"      <Tr><Td Width=118 Align=Right>[ 2]&nbsp;&nbsp;</Td><Td>222 - BBB</Td><Td></Td></Tr>\n"
                 +"      <Tr><Td Width=118 Align=Right>[ 3]&nbsp;&nbsp;</Td><Td>333 - CCC</Td><Td></Td></Tr>\n"
                 +"      <Tr><Td Width=118 Align=Right>[ 4]&nbsp;&nbsp;</Td><Td>444 - DDD</Td><Td></Td></Tr>\n"
                 +"      <Tr><Td Width=118 Align=Right>[ 5]&nbsp;&nbsp;</Td><Td>555 - EEE</Td><Td></Td></Tr>\n"
                 +"      <Tr><Td Width=118 Align=Right>[ 6]&nbsp;&nbsp;</Td><Td>666 - FFF</Td><Td></Td></Tr>\n"
                 +"      <Tr><Td Width=118 Align=Right>[ 7]&nbsp;&nbsp;</Td><Td>777 - GGG</Td><Td></Td></Tr>\n"
                 +"      <Tr><Td Width=118 Align=Right>[ 8]&nbsp;&nbsp;</Td><Td>888 - HHH</Td><Td></Td></Tr>\n"
                 +"      <Tr><Td Width=118 Align=Right>[ 9]&nbsp;&nbsp;</Td><Td>999 - III</Td><Td></Td></Tr>\n"
                 +"      <Tr><Td Width=118 Align=Right>[10]&nbsp;&nbsp;</Td><Td>000 - JJJ</Td><Td></Td></Tr>\n"
                 +"    </Table>\n"
                 +"  </Center>\n"
                 +"<Hr Width=100%>\n"
                 +"</Html>";
    JLabel aLabel=new JLabel(Table);
    aLabel.setFont(new Font("Dialog",0,18));
    aLabel.setOpaque(true);
    aLabel.setPreferredSize(new Dimension(285,413));
    add(aLabel);

  }

  static void Create_And_Show_GUI()
  {
    final Test_JPanel demo=new Test_JPanel();

    JFrame frame=new JFrame("Test_JPanel");
    frame.add(demo);
    frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e)  { System.exit(0); } });
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } }); }
}

enter image description here

Frank
  • 30,590
  • 58
  • 161
  • 244
  • 3
    *"I was told the fonts used in jars apps come from the system, so they look different from the fonts when run from Netbeans"* To the best of my knowledge, ***all*** Java fonts are loaded from the local system. The most likely difference between the two representations is the PLAF - which often do choose different fonts, or different ways of rendering fonts (e.g. anti-aliased). For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). As an aside, what fonts did you discover in your basic debugging? Add code to the MCVE to list font face info. – Andrew Thompson Apr 25 '16 at 01:36
  • A Java runtime (JRE) may [contain any of three families of the Lucida font](http://docs.oracle.com/javase/8/docs/technotes/guides/intl/font.html), in addition to fonts belonging to the host OS. – Basil Bourque Apr 25 '16 at 01:44
  • 1
    Why do you care? Use a layout manager and a logical font family, seen [here](http://stackoverflow.com/a/7189029/230513). – trashgod Apr 25 '16 at 01:58

1 Answers1

0

This is not the answer I was looking for in my question, but this is the answer to get the problem fixed. I found the problem is less about font difference, but more about the html table spacing, I replaced "Cellpadding=1" with "Cellpadding=0" when running in Jar, fixed the problem.

So my app first detects if it's running from IDE or running from Jar, when run from IDE, Cellpadding=1 , when run from Jar, Cellpadding=0, that will make the app look the same in 2 situations.

I hope this can get fixed from the Java side instead of from my app.

Frank
  • 30,590
  • 58
  • 161
  • 244