2

I usually try not to ask questions that I've seen before, but this is different. I've been trying to get my custom font to work, but whenever I try to use it in a method, I get the error "Cannot make a static reference to the non-static method". I know what that means, but I'm not sure how to fix it. Here's the example -

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.io.InputStream;

public class AddFont extends MainFrame{
public void createFont() throws Exception {

InputStream telegraficoFontAdd = AddFont.class.getResourceAsStream(FONT_PATH_TELEGRAFICO);
    Font telegraficoFont = Font.createFont(Font.TRUETYPE_FONT,telegraficoFontAdd);
    telegraficoFont = telegraficoFont.deriveFont(Font.PLAIN,20);
    GraphicsEnvironment ge =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(telegraficoFont);
    }
}

That's my AddFont class file, here is the snippet of my MainFrame -

      public static void main(String[] args) {
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        try {
            AddFont addFont = new AddFont();
            addFont.createFont();
        } catch (Exception e) {
            e.printStackTrace();
        }
        createGUI();

     } //public void run() Closing
  });
}

How would I find a way to use the method class in static objects, because I have a long list of buttons I need to change the fonts for? Or is there some work-around that I must do?

And as always, if you downvote this, please tell me why, so I will know how to improve. Hopefully this is considered an SSCCE.

EDIT - Changed my main method in my MainFrame, now I get the error -

java.io.IOException: Problem reading font data.
at java.awt.Font.createFont(Unknown Source)
at AddFont.createFont(AddFont.java:11)
at MainFrame$1.run(MainFrame.java:107)

(Line 11 in my AddFont file is this -

  Font telegraficoFont = Font.createFont(Font.TRUETYPE_FONT,telegraficoFontAdd);

and Line 107 of my MainFrame is

 addFont.createFont();
Charles
  • 50,943
  • 13
  • 104
  • 142
Hathor
  • 187
  • 1
  • 2
  • 10
  • *"Hopefully this is considered an SSCCE."* An SSCCE is one source file, as opposed to one source file & a code snippet. And since I'm here.. 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) There is never a need for more than blank one line of white space in source. – Andrew Thompson May 18 '13 at 04:48
  • `AddFont.createFont();` <- you are making a static reference to a non-static method. Fix that and your g2g. – Perception May 18 '13 at 04:52
  • To Ravi Tridedi, I know what the problem is. But my main method is static. I want to know if there is a way to change the font class to work with static methods without having to change all of my methods/classes. To Andrew Thompson, I'll try to edit my post so it's just one file, not 2. The two blank likes under createGUI(); I never edited out were accidental, sorry. – Hathor May 18 '13 at 04:55
  • @Hathor, Create an `Object` of your class and then call method `createFont()`. Check out the answer. – Ravi Trivedi May 18 '13 at 04:58
  • @RaviTrivedi I made an edit to my main post, may you take a look at my problem? – Hathor May 18 '13 at 05:20

1 Answers1

1

This function public void createFont() needs to be static if you are planning to call your function using your Class name.

Make it public static void createFont()

If you don't want to use static then simply create Object of your class and then call as below:

AddFont addFont = new AddFont();
addFont.createFont();
Ravi Trivedi
  • 2,340
  • 2
  • 14
  • 20