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:
Related
I am trying to change the properties of a String made from TextField::setPromptText using JavaFX.
This code:
TextField shapeField = new TextField();
shapeField.setMaxWidth(100);
shapeField.setPromptText("Circle, Triangle, Hexagon");
TextField fillField = new TextField();
fillField.setMaxWidth(100);
fillField.setPromptText("Red, Green, Grey");
Currently sets the prompt text to this:
I want to be able to make the font smaller and change the colour. How would I do this?
For color you need:
-fx-prompt-text-fill: red;
and you can change font size with but you need to add an event to check if text is empty then use your prompt text size and when not the actual size of the text (edited):
-fx-font-size
for example:
tf.setStyle("-fx-prompt-text-fill: red;\n" + "-fx-font-size: 26pt;");
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.
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 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.
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));