String.Format is misaligned horribly for some reason - java

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.

Related

Change preview font in table?

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.

Any JTextField descendant or replacement who covers more Chinese characters?

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.

Java (JLabel spacing)

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.

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:

JFreeChart - wrong chart font

I have a problem with XYLineChart. I don't know how to set the chart font to look like Swing components. When I use this:
chart.setTitle(new TextTitle("Tahoma title, style plain, size 11", new Font("Tahoma", Font.PLAIN, 11)));
It is still wrong :(
EDIT: When I create the chart in a new frame, the font in the title is good. How do I set all the labels, axis titles, and other texts to the same font size, without bold?
SOLVED :)
public static void changeStyle(JFreeChart chart) {
final StandardChartTheme chartTheme = (StandardChartTheme)StandardChartTheme.createJFreeTheme();
final Font font = new Font("Tahoma", Font.PLAIN, 11);
final Color color = new Color(0, 0, 0);
chartTheme.setExtraLargeFont(font);
chartTheme.setLargeFont(font);
chartTheme.setRegularFont(font);
chartTheme.setSmallFont(font);
chartTheme.setAxisLabelPaint(color);
chartTheme.setLegendItemPaint(color);
chartTheme.setItemLabelPaint(color);
chartTheme.apply(chart);
}
If you want to change the existing title's font, do something like this:
chart.getTitle().setFont(new Font("Tahoma", Font.PLAIN, 11));
Addendum:
How do I change all items' fonts, e.g. labels, axis, etc.?
StandardChartTheme offers this capability by operating on individual chart components.
I don't know 2 things about XYLineCharts, but it looks like you might be setting the font on the wrong thing. Try setting it on different components.
Not much, but might get you going.
Good luck ;)
Damo

Categories