At run-time, we can use Font.canDisplayUpTo(String) to determine which of the installed fonts can display a given text. Logical fonts such as Font.SANS_SERIF
and Font.SERIF
typically are made of of other fonts and can cover vast ranges of different scripts.
Here is an example using the given text, with the results seen on this machine.
BTW - Google translate is telling me that is Slovenian rather than Croation, but fortunately, the exact same technique will work for any script.
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;
import java.util.Vector;
public class CroationTextInGUI {
private JComponent ui = null;
private String text = "Bohinjska Češnjica";
CroationTextInGUI() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
String[] fontFamilies = GraphicsEnvironment.
getLocalGraphicsEnvironment().
getAvailableFontFamilyNames();
Vector<String> croatFreindlyFonts = new Vector<String>();
for (String name : fontFamilies) {
Font font = new Font(name, Font.PLAIN, 20);
if (font.canDisplayUpTo(text)<0) {
croatFreindlyFonts.add(name);
}
}
final JList list = new JList(croatFreindlyFonts);
list.setVisibleRowCount(20);
list.getSelectionModel().setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
ui.add(new JScrollPane(list), BorderLayout.LINE_START);
final JTextArea output = new JTextArea(text, 2, 12);
output.setLineWrap(true);
output.setWrapStyleWord(true);
ui.add(new JScrollPane(output));
ListSelectionListener showFontListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Font f = new Font(
list.getSelectedValue().toString(), Font.PLAIN, 50);
output.setFont(f);
}
};
list.addListSelectionListener(showFontListener);
list.setSelectedIndex(0);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
CroationTextInGUI o = new CroationTextInGUI();
JFrame f = new JFrame("Croation Text in GUI");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}