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/
Related
In my program I implemented a table, I recently found out how to change the font size of the table so this is what I did:
It sets the font of the contents in the table to 20, however before I confirm an input with enter or select another cell, the preview is still the standard font and too small
table.setFont(new Font("Tahoma", Font.BOLD, 20));
The preview should also be the same size, that's what I wanna do.
What you need to do is modify the Default Cell Editor like this:
Object dce = jTable1.getDefaultEditor(Object.class);
if(dce instanceof DefaultCellEditor) {
Font font = new Font(jTable1.getFont().getName(), jTable1.getFont().getStyle(), 20); // 20 is your desired font size
((DefaultCellEditor) dce).getComponent().setFont(font);
}
This concept was retrieved from this SO Answer by #Redwine.
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.
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:
My swing app contains a JEditorPane which lets users edit text in a WYSIWYG fashion, including toolbar buttons for bold, italic, and setting fonts, etc.
The content type on the JEditorPane is text/html
The problem: Users want to be able to type tab characters in the editor pane, close the edit dialog (saving HTML text to persistent storage) and see their tabs rendered. However, HTML treats all whitespace runs as a single space.
public class NoteTest {
public static void main(String[] args) {
final JEditorPane editPane1 = new JEditorPane("text/html", "Try typing some tabs");
editPane1.setPreferredSize(new Dimension(400, 300));
JOptionPane.showMessageDialog(null, new JScrollPane(editPane1));
JOptionPane.showMessageDialog(null, new JScrollPane(new JEditorPane("text/html", editPane1.getText()))); // where did the tabs go?
}
}
After typing tabs in the first JEditorPane, we get the HTML text from it, and pass it to a second JEditorPane, which renders the tabs as spaces.
How can I render these tabs? Should I be saving the user-entered content as RTF instead of HTML? Should I parse the HTML and build an HTML table with rows and cells (ugh). Or is there some way to tell swing to render the tab characters as tabs?
For better help sooner, post an SSCCE. This SSCCE does not show the behavior you describe.
Note the 'tab' between the n & g of typin g.
import java.awt.Dimension;
import javax.swing.*;
public class NoteTest {
public static void main(String[] args) {
final JEditorPane editPane1 = new JEditorPane("text/html", "Try typing some tabs");
editPane1.setPreferredSize(new Dimension(400, 300));
JOptionPane.showMessageDialog(null, new JScrollPane(editPane1));
JOptionPane.showMessageDialog(null, new JScrollPane(new JEditorPane("text/html", editPane1.getText()))); // where did the tabs go?
}
}
There were some tabs at the end that disappeared, but that makes sense, since tabs are not supported in HTML correctly unless included in a pre element. I'm guessing that the Swing HTML parsing ignores them as redundant.
What if you use < PRE > < /PRE > tags for the text and for the tab char?
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));