Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getFontList()

 
Vikram Deshmukh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
I have some problem with getFontList() function.
It returns list of strings .
But the basic problem is, its an abstract method, in the abstract class
Toolkit.(java.awt.Toolkit)
Since Toolkit is an abstract class, i cannot instantiate its object.
If i decides to override this method, by extending Toolkit,
i'll have to override galaxy of methods, in Toolkit. Even if it is possible with spending long haul, its not an elegant way.
So is there any other way, so i can use getFontList() method, or its functionality ?
Thanx in advance !
from,
vikram.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getFontList is deprecated. The following program list all the fonts on your environment.
If you really want to use getFontList, uncomment those three lines.
<pre>
// import java.awt.Toolkit;
import java.awt.GraphicsEnvironment;
class TestFont02 {
public static void main (String [] args)
{
// String fonts[] = Toolkit.getDefaultToolkit().getFontList();
// for (int i = 0; i < fonts.length; i++)
// System.out.println(fonts[i]);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String allFonts[] = ge.getAvailableFontFamilyNames();
for (int i = 0; i < allFonts.length; i++)
System.out.println(allFonts[i]);
System.exit(0);
}
}
</pre>
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikram,
YOu can get the Fonts list available to the local system through GraphicsEnvironment class. In fact GraphicsEnvironment class is also an abstract class as Toolkit. I am avoiding Toolkit because its method getFontList() is deprecated. But you can ask , then how can I instantiate the GraphicsEnvironment class ?.
From the Java source
-------------------------------
<pre>
public abstract class GraphicsEnvironment {
private static GraphicsEnvironment localEnv;
/**
* This is an abstract class and cannot be instantiated directly.
* Instances must be obtained from a suitable factory or query method.
*/

protected GraphicsEnvironment() {
}
/**
* Returns the local <code>GraphicsEnvironment</code>.
* @return this <code>GraphicsEnvironment</code>.
*/
public static GraphicsEnvironment getLocalGraphicsEnvironment() {
if (localEnv == null) {
String nm = (String) java.security.AccessController.doPrivileged
(new sun.security.action.GetPropertyAction
("java.awt.graphicsenv", null));
try {
localEnv =
(GraphicsEnvironment) Class.forName(nm).newInstance();
} catch (ClassNotFoundException e) {
throw new Error("Could not find class: "+nm);
} catch (InstantiationException e) {
throw new Error("Could not instantiate Graphics Environment: "
+ nm);
} catch (IllegalAccessException e) {
throw new Error ("Could not access Graphics Environment: "
+ nm);
}
}
return localEnv;
}
</pre>

For Every platform the JVM creates a GraphicsEnvironment object when you call the static getLocalGraphicsEnvironment() method on GraphicsEnvironment class From this object you call all the methods like getAllFontFamilyNames()/getAllFonts() etc. This is how we get the Font list. Here is a sample program.
<pre>
import java.awt.*;
class tst {
public static void main(String[] args) {
String[] fontlist = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(int i=0; i<fontlist.length; i++)>
System.out.println(fontlist[i]);
System.out.println("-------------");
Font[] fontlist1 = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for(int i=0; i<fontlist1.length; i++)>
System.out.println(fontlist1[i].getName());
}
}
</pre>
Partial Output (The list is too long )
Abadi MT Condensed
Abadi MT Condensed Light
Arial
Arial Black
Arial Cursiva
Arial Narrow
Arial Narrow Italic
Arial Narrow fed
Arial Narrow fed kursiv
Arial Narrow kursiv
Arial Negreta
Arial Negreta cursiva
Book Antiqua
Bookman Old Style
Bookman Old Style fed
Bookman Old Style fed kursiv
Bookman Old Style kursiv
Bookshelf Symbol 1
Bookshelf Symbol 2
Bookshelf Symbol 3
---------------------------------------------------------

[This message has been edited by maha anna (edited May 09, 2000).]
 
Vikram Deshmukh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic