Any JTextField descendant or replacement who covers more Chinese characters? - java

I'm working on an application, which uses a JTextfield object as a text input component. During some tests I've seen that not all Chinese characters can be shown using this component.
At first this seems normal: some Chinese characters are that large that they are not even covered by Unicode, which (in my humble opinion) explains why they can't be copied to clipboard (only as a bitmap, which means that font modifications can't be tested).
However, there also seem to be characters, like the four dragon character (explained in URL Largest Chinese character in Unicode) which can be copied to clipboard, but which seems not to be accepted by the JTextField object.
Hence my question: is there any descendant of JTextField which covers all Chinese characters, or at least the ones present in Unicode? Or is anybody aware of another, more powerful component?
Thanks in advance

With JTextField you would need to specify a font that can support all the characters that you want to use.
This may be impossible if you want to support a large number of characters. Example, the font MingLiU-ExtB supports some uncommon Chinese characters like 𪚥, but it does not support common Chinese characters like 漢字.
Fonts like Arial Unicode MS or MingLiU support common characters, but not uncommon ones.
So, it may be impossible with JTextField.
But, JTextPane is more flexible, at least when running on Windows. If it needs some characters that are not in the font you have specified, it will add characters from other fallback fonts. (I have no idea what algorithm it uses to select the other fonts. This behavior might be coming from Windows itself, and not from Java: See discussion of Font Fallback here.)
This code gives a few examples of JTextField and JTextPane with a few fonts.
public class ChineseFont {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
String text = "test 𪚥 test 漢字.";
JTextField textField1 = new JTextField(text);
textField1.setFont(new Font("Arial Unicode MS", Font.PLAIN, 24));
JLabel label1 = new JLabel("JTextField " + textField1.getFont().getFontName());
JTextField textField2 = new JTextField(text);
textField2.setFont(new Font("MingLiU", Font.PLAIN, 24));
JLabel label2 = new JLabel("JTextField " + textField2.getFont().getFontName());
JTextField textField3 = new JTextField(text);
textField3.setFont(new Font("MingLiU-ExtB", Font.PLAIN, 24));
JLabel label3 = new JLabel("JTextField " + textField3.getFont().getFontName());
JTextPane textPane4 = new JTextPane();
textPane4.setFont(new Font("Arial Unicode MS", Font.PLAIN, 24));
textPane4.setText(text);
JLabel label4 = new JLabel("JTextPane " + textPane4.getFont().getName());
JTextPane textPane5 = new JTextPane();
textPane5.setFont(new Font("MingLiU", Font.PLAIN, 24));
textPane5.setText(text);
JLabel label5 = new JLabel("JTextPane " + textPane5.getFont().getName());
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(5, 2, 2, 6));
contentPane.add(label1);
contentPane.add(textField1);
contentPane.add(label2);
contentPane.add(textField2);
contentPane.add(label3);
contentPane.add(textField3);
contentPane.add(label4);
contentPane.add(textPane4);
contentPane.add(label5);
contentPane.add(textPane5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
});
}
}
You can also manually specify fonts for each section of your text using AttributedText. See this answer

Like #Enwired said, use a font that supports more characters. Fonts are just pictures that contain all of the characters and info on how to display them. So a font with more characters or a font designed for Chinese characters will work much better. This site works very well for finding fonts. It has many that may suit your needs.

Related

How do I to display "sigma-hat" using Unicode or any alternative in Java? (I have a partial solution)

I'm trying to produce a sigma-hat symbol (for sample standard deviation).
On a Windows 7 system, the following code produces a JLabel with a misaligned sigma hat:
JLabel sigmaHat = new JLabel("\u03C3\u0302");
And it looks like this:
http://i.imgur.com/z4Nowwm.jpg
Am I using the wrong combining character or is the Unicode for sigma-hat broken? Also, is it possible to produce a symbol for sample variance (sigma-hat-squared)?
Using U+0302 COMBINING CIRCUMFLEX ACCENT after the sigma character is the correct way. In Unicode, a combining mark appears after the base character in the data stream. And there is no other combining mark that could be conceivably used instead.
However, the result depends on the font(s) and on the rendering engine. Failures are common. Testing here: σ̂. (Does not look good.) Trying different fonts, when possible, may help. But in general, notations like this are usuall written using equation editors, LaTeX, or other tools that operate above the plain text level.
Out of curiosity I wrote up a test:
import javax.swing.*;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.EventQueue;
public class FontCheck{
final static String string = "\u03C3\u0302";
public static void main(String[] args){
EventQueue.invokeLater(()->{
JFrame frame = new JFrame("font check!");
JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
content.add(new JLabel(string));
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(String font: fonts){
JLabel label = new JLabel(font + " : " + string);
label.setFont(new Font(font, Font.PLAIN, 12));
content.add(label);
}
frame.setContentPane(new JScrollPane(content));
frame.setSize(400, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
That shows all of the available fonts, and the first one is the default font. (Which works fine for me.)

Set the font of texts retrieved from JTextField

I have the user enter some words through a JTextField. I want to set the font of the string. Here is what I have so far.
Font f;
f = new Font(input.getText(), Font.ITALIC, 32);
word = new JLabel(f, SwingConstants.CENTER);
Unfortunately, Java is throwing me a compiler error because JLabel doesn't accept Font as a parameter. Is it possible to set the font of a string retrieved from a text field and have it displayed on a JFrame?
See JComponent.setFont(Font).
Sets the font for this component.
But better than a text field to set a font name, see this answer for a (styled) combo or this answer that uses a list:

Write Indian regional languages like Gujrati, Punjabit etc. in JTextPane

I am able to write Hindi, Urdu in JTextPane, but not able to write other Indian regional languages in text pane. I have also downloaded the font for these languages, but it doesn't work.
How to write Indian regional languages like Gujrati, Punjabit etc in JTextPane?
UPDATE :
A piece of code as requested :
public class NewClass {
public static void main(String args[]) {
JFrame j = new JFrame("Hello!");
j.setSize(200, 200);
JTextPane k = new JTextPane();
k.setFont(new Font("Shree-Guj-0768W", Font.PLAIN, 17));
j.add(k); j.setVisible(true);
}
}
I have set the gujrati(a Language) font in jtextpane, the existing content appear in gujrati,but when i write in the jtextpane boxes appears. Can we have multiple indian regional languages in same Jtextpane?
First thing is to install the font.
Secondly set the font for JTextPane -
for e.g if you want to set Shivaji05 font for typing Marathi in JTextPane then use:
jTextPane1.setFont(new java.awt.Font("Shivaji05", Font.PLAIN, 11));
You need to set the font for JTextPane.
Here is the link below how you can set the font and used it
http://www.javaprogrammingforums.com/java-swing-tutorials/39-how-change-jtextarea-font-font-size-color.html
http://javatechniques.com/blog/setting-jtextpane-font-and-color/

How to set String's font size, style in Java using the Font class?

Suppose I have a String, "Hello World". I want to change the style of this string to a BOLD font, and set the size of all the characters from 12 to 18 [pt]. After that I want to use this string in a JLabel and JButton. How can I do that?
Font myFont = new Font("Serif", Font.BOLD, 12);, then use a setFont method on your components like
JButton b = new JButton("Hello World");
b.setFont(myFont);
Look here http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#deriveFont%28float%29
JComponent has a setFont() method. You will control the font there, not on the String.
Such as
JButton b = new JButton();
b.setFont(b.getFont().deriveFont(18.0f));

How to fit a long String into a JLabel

as the title says: I need to fit a JLabel into a JFrame, but the text in the JLabel are too long, so I need to add some linebreaks. The text in the JLabel are obtained from an online XML file, so i cant just change the text to contain linebreaks.
This code extracts data from the XML-file
Element element = (Element)nodes1.item(i);
String vær = getElementValue(element,"body");
String v = vær.replaceAll("<.*>", "" );
String forecast = "Vær: " + v;
in this case the string I want to add some linebreaks to the string v. The String v contains the parsed data from the xml file. The String forecast is returned and set as a text to the JLabel.
Just ask if something is uncleared, thanks in advance!
I suggest using a JTextArea instead and turning wrapping on. The only way to do it in a JLabel is to put line breaks <br />, which wouldn't work (at least not easily) in your situation if you don't know the text beforehand.
JTextArea is much more flexible. By default it looks different, but you can fiddle around with some of the display properties to make it look like a JLabel.
A simple modified usage example taken from the How to Use Text Areas tutorial -
public class JTextAreaDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame("JTextArea Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
JTextArea textArea = new JTextArea(
"If there is anything the nonconformist hates worse " +
"than a conformist, it's another nonconformist who " +
"doesn't conform to the prevailing standard of nonconformity.",
6,
20);
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setOpaque(false);
textArea.setEditable(false);
panel.add(textArea);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
JLabel is able to display HTML text, i.e. if you wrap your text with <html>your text<html> it might be able to wrap the text. That's not tested though, so YMMV.
you can dynamically tell your JLabel to resize itself to fit the text.
if you are not using a LayoutManager try :
jLabel.setText ("A somewaht long message I would not want to
stop");
jLabel.setSize(jLabel.getPreferredSize());
If you are using a Layout Manager this snippet should work:
jLabel.setText ("A somewaht long message I would not want to
stop");
jLabel.validate();

Categories