aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes getFontList() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "getFontList()" Watch "getFontList()" New topic
Author

getFontList()

Vikram Deshmukh
Greenhorn

Joined: Apr 22, 2000
Posts: 29
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Apr 22, 2000
Posts: 29
Thanx !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getFontList()