I want the JLabel to display "hi, my name is Bob"
However, when I coded:
JLabel.setText("hi, my name is Bob");
the spaces are "consumed" and the output will read
"hi, my name is bob"
Can anyone please help me on this?
Thanks in advance
Here are two ways that both have the same effect. One sets the font to MONOSPACED while the other marks the text as HTML preformatted (which also uses a monospaced font & preserves spaces and tabs).
String TEXT = "hi, my name is Bob";
// ...
JLabel l = new JLabel(TEXT);
l.setFont(new Font(Font.MONOSPACED, Font.PLAIN, l.getFont().getSize()));
ui.add(l);
ui.add(new JLabel("<html><body><pre>" + TEXT));
Having said that, I agree with #Madonah & #maraca that this is best handled in two labels, using layouts (borders and padding) to achieve the required result.
Related
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.
How to setLineWrap, I'm according to https://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html#setLineWrap%28boolean%29
but how I can setLineWrap to jlabel, I have something like this:
String a = "text (...)";
JLabel label = new JLabel(a);
but my text is leaving
I mean:
JLabel:
aaaaaaaaaaaaaaaaaaaaaaaxxxxxxx where a is text and x is text that disappeared
JTextArea:
aaaaaaaaaaaaaaaaaaaaaaa
aaaaaaa
There is no setLineWrap method in JLabel. But if you set HTML to the JLabel you can overcome this.
JLabel l = new JLabel("<html><p>line 1</p><p>line 2</p></html>");
You can actually use a JTextField and make it readonly to look like a Label. When you make the text field readonly, the long text can be scrollable using keyboard.
JTextField txtLabel = new JTextField();
txtLabel.setEditable(false)
txtLabel.setText("aaaaaaaaaaaaaaaaaaaaaaaxxxxxxx");
If you want the text to wrap you may want to use the JTextArea by making it readonly with a label look and feel.
I have Label in SWT
The text is
"Doctor\ntest\ttest2"
I want to change it to
"Doctor
test test2"
menaing to change the \n to new line and \t to tab.
Can I do it in Label in SWT? Do I need to change the control ?
You can do this in the label control.
final Label label = new Label(composite, SWT.NULL );
label.setText( "Line one \n line two abc \t tabbed" );
label.pack();
Results in:
You can also do this by using a Text-Control and set it disabled.
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:
I'm used to using printf, but my research led me to believe I could use String.Format for setting up tables in a JTextArea and that it was essentially the same thing. This is for a rhythm game app. My code is:
private final String HEADER = "%-10s%-15s%-90s%-9s%-6s%-9s%-7s%-6s%-9s";
...
ranks.setText("");
ranks.append(String.format(HEADER + "\n", "Rank", "Difficulty", "Song Name", "Perfects", "Goods", "Averages", "Misses", "Boos", "MaxCombo"));
ranks.append(analyze.toString());
...
return String.format("%-10d%-15d%-90s%-9d%-6d%-9d%-7d%-6d%-9d\n ", rank, difficulty, songName, perfects, goods, averages, misses, boos, maxCombo);
for each component in an array analyze.toString returns the string shown. My format strings are identical sans everything being strings in the header and most everything being integers in the table so I don't know why my table comes out looking like this:
Like stated in the comments you should use a fixed-width font (or monospaced font) for such alignments to work.
JTextArea area = new JTextArea();
area.setFont(new Font("Monospaced", Font.PLAIN, 12));
Or as stated Denis Tulskiy, directly use the JTable component.