I need to display a multiline, read-only text - which control can be used for that? It should only display the text, like a Label does, however Label does not support multiline?
Thanks for any hint :-)
You can display multi-line read-only text in a Label.
If the text has \n (newline) characters in it, then the label will wrap to a new line wherever the newline character is.
If the label has wrapText set to true and there is not enough space to display a single line of text, then the label will wrap the text to a new line.
If you want text in different styles, then, if using JavaFX 2, place multiple labels in a FlowPane or, if you are using Java 8+, place the labels in a TextFlow component.
Produced by the following code:
import javafx.scene.Scene;
import javafx.application.Application;
import javafx.scene.control.Label;
import javafx.scene.image.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class LoveMeNot extends Application {
public static void main(String[] args) throws Exception { launch(args); }
#Override public void start(final Stage stage) throws Exception {
Label label = new Label(WORDS);
label.setWrapText(true);
label.setStyle("-fx-font-family: \"Comic Sans MS\"; -fx-font-size: 20; -fx-text-fill: darkred;");
ImageView image = new ImageView(IMAGE);
image.setOpacity(0.3);
StackPane layout = new StackPane();
layout.setStyle("-fx-background-color: mistyrose; -fx-padding: 10;");
layout.getChildren().setAll(image, label);
stage.setTitle("Love Me Not");
stage.setScene(new Scene(layout));
stage.show();
}
// creates a triangle.
private static final String WORDS =
"Love not me for comely grace,\n" +
"For my pleasing eye or face,\n" +
"Nor for any outward part,\n" +
"No, nor for my constant heart,\n" +
"For these may fail, or turn to ill.\n" +
"So thou and I must sever.\n" +
"Keep therefore a true woman’s eye,\n" +
"And love me still, but know not why,\n" +
"So hast thou the same reason still\n" +
"To doat upon me ever.";
private static final Image IMAGE =
new Image("http://icons.iconarchive.com/icons/artdesigner/gentle-romantic/256/rose-icon.png");
}
Try running the above program and resizing the window to see the effect of the \n new line values in the label's text as well as the wrapText property on the label. Vary the wrapText setting from true to false to see the difference between having wrapText switched on and off.
If you set a max width you want for your Label, then you set setWrapText to true so it displays the text multiline:
Label label = new Label("Your long text here");
label.setMaxWidth(180);
label.setWrapText(true);
You can also use Text to appear in multiline, by setting wrappingWidthProperty according to your needs.
Text text = new Text();
text.wrappingWidthProperty().set(345);
In this code, I have set max width to 345.0 ,So When the text's size reaches beyond 345 pixels will be wrapped to next Line.
If the text has \n (newline) characters in it, then the label will place wrap to a new line wherever the newline character is.
It not works when you set text from FXML file as well as from visual FXML editor.
Text class also available. A good explanation in this answer label-and-text-differences-in-javafx Basically use Text for where you're not getting input, otherwise Label is better.
Related
When a TextField cannot display all the text of its contents, I would like an ellipsis (… HORIZONTAL ELLIPSIS) to appear as the last character displayed to indicate to the user that some of the field contents is not visible. Actually, any kind of indicator would be acceptable, ellipsis or otherwise.
➛ Is there such a feature in Vaadin 8 (Framework) or 10 (Flow)?
➛ Is there some workaround or modification I can make to do this? Some CSS trick?
I'm not using v10 but I assume a similar approach as the one below based on v8 should get you the same effect. One thing to note is that, like a ton of other stuff, it depends on the browser you're using, and for IE & Edge (link1 pointing to link2) you need to make the input read-only to have it work, hence the focus/blur listeners in my sample. If you'd like, you can further style the input to make it look like a regular one if disabled, but that's not the main point here.
Code
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
public class MyEllipsisTextFieldComponent extends VerticalLayout {
public MyEllipsisTextFieldComponent() {
TextField ellipsisTextField = new TextField("Ellipsis", "This is a text field with a custom style that uses ellipsis to display very long and uninteresting texts like this one");
ellipsisTextField.addStyleName("ellipsis");
ellipsisTextField.addFocusListener(event -> ellipsisTextField.setReadOnly(false));
ellipsisTextField.addBlurListener(event -> ellipsisTextField.setReadOnly(true));
ellipsisTextField.setReadOnly(true);
addComponent(ellipsisTextField);
addComponent(new TextField("No ellipsis", "This is a regular text field with a very long and uninteresting text that does not use ellipsis"));
}
}
Theme
.v-textfield-ellipsis {
text-overflow: ellipsis;
overflow: hidden;
}
Result
I've got some really big problems with AcroForm CheckBox.
1. CheckBox (in checked state) is not printed by Adobe Reader DC. Even after setting the corresponding FieldFlag. Maybe this is because I have'nt got the font ZapfDingsbats on my Computer?
2. How do I change this font (I want to use Wingdings) for all the symbols (On/Off state) used by the different appearances of the checkbox?
3. Last but not least: How to draw a border around the unchecked CheckBox?
Thanks a lot for every tip and help!
package jumpstart;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
public class Problem6 {
public static void main(String[] args) throws IOException {
PdfWriter writer = new PdfWriter("problem6.pdf");
PdfDocument pdf = new PdfDocument(writer);
PdfPage page = pdf.addNewPage();
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
PdfButtonFormField checkBox = PdfFormField.createCheckBox(pdf, new Rectangle(75, 750, 20, 20), "cbName1", "On",
PdfFormField.TYPE_CROSS);
// checkBox.setFieldFlag(PdfAnnotation.PRINT);
form.addField(checkBox, page);
pdf.close();
Desktop.getDesktop().open(new File("problem6.pdf"));
}
}
Part 1. To make the PdfFormField printable you should use the following method:
checkBox.setVisibility(PdfFormField.VISIBLE);
Part 2. In your particular case the appearance of the checkbox is drawn using canvas instructions, not any fonts. This is how it's done:
q
Q
0 20 m
20 0 l
20 20 m
0 0 l
S
Part 3. As you can see, no fonts are used, the cross is drawn with lines.
Thus, it's not possible easily to use different font. You can create your own appearance stream and set it via PdfFormField#setAppearance. But you have to be very careful and know what you are doing.
You can set field border with these lines:
checkBox.setBorderWidth(1);
checkBox.setBorderColor(ColorConstants.BLACK);
But this would set borders for all the appearances, including On and Off. If you want the border to be drawn only for Off appearance, you should create your custom appearance and use PdfFormField#setAppearance.
P.S. Please ask questions separately. The three questions you asked are different ones.
Is it possible to directly change the font size of the text in an editable text box?
I have a tutorial stage where a message is displayed at the end when the user completes the tutorial.
I call this message endingText which is defined in my TutorialDefinition script:
string endingText = "You have completed the tutorial stage!";
In another script called TutorialEditor, I initialize it with the text that the user can input through the Editor. So, the endingText variable is there in the Definition script in case the user did not input anyhing...
void initValues()
{
endingText = stage.EndingText;
}
And finally, when the tutorial is done through the TutorialExecutor, I show the message, using:
endingText = GUILayout.TextArea( endingText );
The font and font size are set somewhere else through the current skin I think, but I wonder if it is possible to directly manipulate the above code snippet and change the font size?
I mean this line of code: endingText = GUILayout.TextArea( endingText ); that will eventually show the user the final message through void OnGUI() method...
(I understand I could if, instead of endingText, there was actual text within "" but what about now?)
I am very curious to know how "Print" button capture current screen? When we press "print" button what happens? How it'll capture the screen?
Please let me know if anyone know about the same.
Thanks, Jimit
ORIGINAL USE
Under command-line based operating systems such as MS-DOS, this key causes the contents of the current text mode screen memory buffer to be copied to the standard printer port, usually LPT1. In essence, whatever was currently on the screen when the key was pressed was printed. Pressing the Ctrl key in combination with Prt Sc turns on and off the "printer echo" feature. When echo is in effect, any conventional text output to the screen will be copied ("echoed") to the printer. There is also a Unicode character for print screen, U+2399 ⎙
.
MODERN USE
Newer-generation operating systems using a graphical interface tend to copy a bitmap image of the current screen to their clipboard or comparable storage area, which can be inserted into documents as a screenshot. Some shells allow modification of the exact behavior using modifier keys such as the control key.
Macintosh does not use a print screen key; instead, key combinations are used that start with ⌘ Cmd+⇧ Shift.
.
Coding
For example a C# code can run to take a screenshot:
private void PrtScr() {
Bitmap bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bm as Image);
g.CopyFromScreen(0, 0, 0, 0, bm.Size);
bm.Save(#"C:\image.jpeg", ImageFormat.Jpeg);
}
For example Java code:
class ScreenRecorder {
public static void main(String args[]) {
try {
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension d = tool.getScreenSize();
Rectangle rect = new Rectangle(d);
Robot robot = new Robot();
Thread.sleep(2000);
File f = new File("screenshot.jpg");
BufferedImage img = robot.createScreenCapture(rect);
ImageIO.write(img,"jpeg",f);
tool.beep();
} catch(Exception e){
e.printStackTrace();
}
}
}
I have an incoming text string that contains a line break ("\r").
When I output it with : System.out.println(myString), the carriage return is interpreted.
However, when I set the string as the Label's content, it ignores the carriage return.
How can I force the Label to interpret the carriage/line return (without the XHTML mode) ?
This is how you can put this text into your label:
#Override
public void init() {
Window window = new Window();
Label label = new Label("<pre>First line\rSecond line\nThird line</pre>", Label.CONTENT_XHTML);
window.addComponent(label);
setMainWindow(window);
}
The key is using Label.CONTENT_XHTML content mode and enclosing the text inside a <pre> tag.
In Vaadin 7.0 you can use ContentMode.PREFORMATTED e.g.:
String resultText = "First line\rSecond line\nThird line";
Label dateLabel = new Label( resultText, ContentMode.PREFORMATTED );
and if you want text to look sexy you can use some themes, something like:
dateLabel.setStyleName( Runo.LABEL_SMALL );
That should work and is elegant as well.
After reading The book of Vaadin, and few tests, I don't think \r can be interpreted by the Label.
Replacing \r with \n gives you two options :
Label.setContentMode(Label.CONTENT_XHTML). //But you don't want to do this
Label.setContentMode(Label.CONTENT_PREFORMATTED) //But I think it's not the display you want
Regards.
Éric