2

I am creating a digital clock using Java and I have done that. Now I want to set the font of digital clock in the JLabel which is showing the time. I am applying the font with the help of following code.

try {
    Font f1= new Font("Digital-7" ,Font.BOLD,28);
    jLabel1.setFont(f1);
} catch(Exception ex) {
    ex.printStackTrace();
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
adesh singh
  • 1,727
  • 9
  • 38
  • 70

1 Answers1

9

+1 HovercrafFullOfEels comments.

I dont think I have seen Digital-7 font pre-installed (well at least not on Windows OS) perhaps you need the font file, you would than load the font file and only than can it be used.

See below for loading a font file and than using it (I have omitted error handling for readability):

String filename="path/to/file/whatever.ttf";//this is for testing normally we would store the font file in our app (knows as an embedded resource), see this for help on that http://stackoverflow.com/questions/13796331/jar-embedded-resources-nullpointerexception/13797070#13797070

Font font = Font.createFont(Font.TRUETYPE_FONT, new File(filename));
font = font.deriveFont(Font.BOLD,28);

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);

JLabel l = new JLabel("Some Text");
l.setFont(font);
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • @nIcEcOw +1 hehe Thank you :)... and when will you be joining? Just gotta Stay up a few late nights :P – David Kroukamp Jul 01 '13 at 17:27
  • You're MOST WELCOME and KEEP SMILING :-). Now a days learning C and C#, LOL, finding that a bit hard to grasp :-), so seldom find time to answer Java questions :( – nIcE cOw Jul 01 '13 at 17:32
  • @nIcEcOw Lol C?!!! Ahh I want to learn that too, but yup, the syntax already confuzzles me. As for C#, when I get my skills up to standards Im sure to find you roaming the C# forum answering questions :) – David Kroukamp Jul 01 '13 at 20:49