[Originally posted by fritb] I'm having a couple of problems using native fonts with JDK 1.2.2 on Windows NT. As shown in the program below, once you set a given native font for a component, if you then set a new font with the same name and point size, but a different style, the new style will not be put into effect. This is not the case for a standard font (serif, sanserif, monospaced, dialog). The other problem is that without the call to getAvailableFontFamilyNames() for the local graphics environment, I can't get the right native font even if I specify it correctly by name. On my system, if I call setFont using a native font without first calling getAvailableFontFamilyNames(), it will always use Arial. It appears that getAvailableFontFamilyNames() does some sort of initialization that allows native fonts to be selected correctly, but the JDK documentation says nothing about this requirement. Are these bugs, or am I missing something? import javax.swing.*; import java.awt.event.*; import java.awt.*; public class FontTest2 extends JFrame { static public void main(String[] args) { FontTest2 ft = new FontTest2(); ft.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); ft.getContentPane().setLayout(new GridLayout()); // Without calling this, Arial is always used even when a different native // font is requested. // GraphicsEnvironment.getLocalGraphicsEnvironment() .getAvailableFontFamilyNames(); // Set up a text field using a standard font and plain style. // JTextField textfield1 = new JTextField(); ft.getContentPane().add(textfield1); textfield1.setFont(new Font("serif", Font.PLAIN, 24)); textfield1.setText(textfield1.getFont().getFamily()); // Set up a text field using a native (Windows) font and plain style. // JTextField textfield2 = new JTextField(); ft.getContentPane().add(textfield2); textfield2.setFont(new Font("arial", Font.PLAIN, 24)); textfield2.setText(textfield2.getFont().getFamily()); ft.setSize(400, 100); ft.setVisible(true); // Wait a couple of seconds so effect of font change can be observed. // try { Thread.sleep(2000); } catch (InterruptedException iex) {} // Now try to change style to italic for both text fields; this works for // the standard font, but not the native font. However, if the point size // is also changed for the native font, both it and the style change WILL // take effect. // textfield1.setFont(new Font("serif", Font.ITALIC, 24)); textfield2.setFont(new Font("arial", Font.ITALIC, 24)); } }
|