I'm trying to create a cross-platform swing application, and will include at least 3 Unicode fonts (say Japanese, Tamil and Hebrew, all belonging to different families). They're supposed to be used in a component (say JTable) when there is either Japanese, Tamil or Hebrew font (none of the two will be present simultaneously in cells). Is there any way to detect glyph and set font family accordingly?
Assuming the text appears in a JTable, you can write a custom cell renderer that will switch the font depending on the text to be displayed.
For example:
JTable table = new JTable();
table.setModel(model);
table.setDefaultRenderer(String.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
String text = (String) value;
if (text.matches(".*[\\u3040-\\u30FF]+.*")) { // Japaneese
c.setFont(font1);
} else if (text.matches(".*[\\u0B80-\\u0BFF]+.*")) { // Tamil
c.setFont(font2);
} else if (text.matches(".*[\\u0590-\\u05FF]+.*")) { // Hebrew
c.setFont(font3);
} else {
c.setFont(table.getFont());
}
return c;
}
});
Custom renderers can be used for other components as well (JList, JTree, JComboBox).