My notepad, built with Java JTextPane, works perfectly okay while writing English. However, letters written in other language, in my case Bangla, shows empty squares. How do I solve the problem?
Asked
Active
Viewed 1,976 times
1

Andrew Thompson
- 168,117
- 40
- 217
- 433

Shihab Shahriar Khan
- 4,930
- 1
- 18
- 26
-
1I had this problem before. The answer is you need to use a UTF font – ControlAltDel Apr 25 '16 at 14:05
1 Answers
4
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
are typically made out of combinations of other fonts and can cover vast ranges of different scripts.
Here is an example using Slovenian text, with the results seen on this machine. The list on the left shows the installed fonts which can display the characters.
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);
}
}

Andrew Thompson
- 168,117
- 40
- 217
- 433