JavaFX set label text by css - java

I was wondering if by javafx-css it's possible to set label text.
On the official documentation i found a reference to
-fx-text: "whatever";
but isn't working, basically i'm searching for and equivalent of css3
content: "whatever";
specified here: W3School

This is not possible in JavaFX and you can easily verify this by getting all the styleable properties from a Label:
Label label = new Label();
label.getCssMetaData().stream().map(CssMetaData::getProperty).sorted().forEach(System.out::println);
Which yields the following list (not including -fx-text or anything that allows you to set the content according to the CSS Reference Guide):
-fx-alignment
-fx-blend-mode
-fx-content-display
-fx-cursor
-fx-effect
-fx-ellipsis-string
-fx-focus-traversable
-fx-font
-fx-graphic
-fx-graphic-text-gap
-fx-label-padding
-fx-line-spacing
-fx-max-height
-fx-max-width
-fx-min-height
-fx-min-width
-fx-opacity
-fx-opaque-insets
-fx-padding
-fx-position-shape
-fx-pref-height
-fx-pref-width
-fx-region-background
-fx-region-border
-fx-rotate
-fx-scale-shape
-fx-scale-x
-fx-scale-y
-fx-scale-z
-fx-shape
-fx-skin
-fx-snap-to-pixel
-fx-text-alignment
-fx-text-fill
-fx-text-overrun
-fx-translate-x
-fx-translate-y
-fx-translate-z
-fx-underline
-fx-wrap-text
visibility

Related

How to Embed Font in JavaFX?

How can I embed a custom font in a JavaFX Application? I've tried making a fonts.mf file, and following the instructions to a similar question linked below. I don't want to use CSS if I don't have to. I want to focus on learning core JavaFX stuff.
Here is what I've been messing around with:
private static Label makeTitle() {
Label title = new Label("Bandit King");
Font font = new Font("OldStyle", 40);
title.setFont(font);
return title;
}
my fonts.mf file contains just this line:
OldStyle = /home/myName/Desktop/My_Java_Projects/Bandit_King/banditKing/OLDSH.TTF
This is not a duplicate of this question. Eclipse says, "CustomFontApp, cannot be resolved to a type".
I've tried making a fonts.mf file
You don't need it.
This is not a duplicate of this question. Eclipse says, "CustomFontApp, cannot be resolved to a type".
CustomFontApp is just the name of a class used in the answer you linked - your class name is obviously different, and you should've changed it.
This line:
Font font = new Font("OldStyle", 40);
will load OldStyle font only if it's installed on your system. You aren't using an installed font, but the embedded one, so that won't work.
You need to use Font.loadFont(InputStream, double) or Font.loadFont(String, double) to load your custom font from disk:
// use this to load font from your application's resource folder (`res/fonts/OLDSIH.TTF`)
Font font = Font.loadFont(getClass().getResourceAsStream("/fonts/OLDSIH.TTF"), 40);
// or this one to load font from the specified (absolute) path
// (not recommended, use the method above or, at least, change this into relative path):
Font font = Font.loadFont("file:///home/myName/Desktop/My_Java_Projects/Bandit_King/banditKing/OLDSH.TTF", 40);

JavaFX .setStyle(...) isn't not working when `-fx-background-image` changes

What i want to achieve:
I am using a FileChooser and the user selects an appropriate .jpg image file . Then i am copying that image , renaming it background.jpg to a known folder and trying to set it as the background image of the application using .setStyle(...); There is not problem of copying the image [ i am checking it]
The Problem that occurs:
I have a Stage with a BorderPane . The BorderPane has an background Image , i do that using
borderPane.setStyle("-fx-background-image:url('filepath')");
!It works well the first time!
->Then i am deleting that file [background.jpg] and i am replacing it with another file named also [background.jpg] . The background image of the BorderPane isn't changing ....
I have tried also resetting the same style using again :
borderPane.setStyle("-fx-background-image:url('filepath')");
Finally when i am changing the filename , for example to [background-12.jpg] and reseting the style using the above it changes the background image.
Which exactly is the problem ? I mean i am sure that the background.jpg has been created , i am checking it and also when i am changing the name to something other again and again it works .
Is the Java CSS Parser lazy to parse the new style which is the same but has other -fx-background-image resource ?
As for the File path i am sure it is well , i am using the code below :
//Maou is the File URL in appropriate format for CSS
String maou = file.getAbsoluteFile().toURI().toString()
//Here i add the appropriate file separator, if not JavaFX will report error
maou = maou.replaceAll("\\Q\\\\E", "//");
//Print maou
System.out.println("Maou=\n" + maou);
Solution :
I found as best solution using James_D answer , a little bit modified so it covers the whole window:
BackgroundImage bgImg = new BackgroundImage(image, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
new BackgroundSize(window.getWidth(), window.getHeight(), true, true, true, true));
Rather than using an inline style, I would recommend setting the background via the background property directly:
Image img = new Image(file.getAbsoluteFile().toURI().toString());
BackgroundImage bgImg = new BackgroundImage(img,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
borderPane.setBackground(new Background(bgImg));
The Background class provides Java API programmatic access to all the same properties that can be set by CSS.
While I don't know exactly what's going on, presumably it's some form of caching which JavaFX is doing to try to be "helpful". I may look into the source code later.
To be honest, though, setting a background via CSS feels like the wrong approach to me. I always avoid setting any styles explicitly, like:
borderPane.setStyle("something");
and prefer to add and remove style classes:
borderPane.getStyleClass().add("foo");
borderPane.getStyleClass().remove("foo");
I don't think this is possible in your situation, so I would instead use a StackPane
to layer your content over an ImageView.
ImageView img = new ImageView(new Image(new URL("path")));
StackPane stack = new StackPane();
stack.getChildren.addAll(img, /*overlaid content*/);

passing objects as arguments using namedArg annotation in javafx 8

how do one pass a non-string object as argment using #namedArg in javafx? I cannot find a single nutshell example regarding this question online!
I am currently trying to instantiate a InlineCssTextArea from RichTextFX wrapped in a VirtualizedScrollPane - please have a look at this source code:
public VirtualizedScrollPane(#NamedArg("content") V content) {
[...]
}
where the custom type V is extending Node. In my case, I want to pass InlineCssTextArea as V. Doing this programmatically is pretty easy:
InlineCssTextArea area = new InlineCssTextArea();
Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(area)), 600, 400);
but translating that to FXML is quite challenging. I have already tried a few things, like fx:factory based on the official oracle fxml tutorial:
<VirtualizedScrollPane fx:factory="content">
<InlineCssTextArea />
</VirtualizedScrollPane>
or how #namedArg suggests, as argument:
<VirtualizedScrollPane content="InlineCssTextArea" />
-or-
<VirtualizedScrollPane content="<InlineCssTextArea />" />
Is there a fxml solution for this problem?
my question is based on the the following answer from James D: What is the purpose of #NamedArg annotation in javaFX 8?
You basically need to pass a value for an argument called content. The two ways to pass a value for an argument in FXML are as an attribute: content="..." or using a property element. Using an attribute only works if you can pass a string which the FXML loader knows how to convert to the appropriate value (i.e. if the value is a string or a primitive type), which isn't the case here. Using a property element you just nest an element whose name is the property name and nest the value inside it:
<VirtualizedScrollPane>
<content>
<InlineCssTextArea />
</content>
</VirtualizedScrollPane>

Is it possible to add 'Open a web link action' action to acroform field programmatically using iText?

In Adobe Acrobat there is a possibility to add 'Open a web link action' to acroform. Is it possible to do so with iText usind already existing acroform?
I was unable to find any mention about it at iText docs and therefore tried to create new acrofield programmatically and add this action to it, but without success. Here's my code:
PdfReader pdfReader = new PdfReader(templateStream);
PdfStamper stamper = new PdfStamper(pdfReader, new FileOutputStream("delivery.pdf"));
stamper.setFormFlattening(true);
stamper.getAcroFields().setField("package", packages);
stamper.getAcroFields().setField("purchase_id", purchaseId);
stamper.getAcroFields().setField("activation_code", activationCode);
if (partner != "") {
PdfFormField field = PdfFormField.createTextField(stamper.getWriter(), false,
false, 100);
field.setFieldName("partner");
PdfAction action = new PdfAction(partner);
field.setAction(action);
field.setColor(new BaseColor(0,0,255));
PdfAppearance appearance = stamper.getUnderContent(1).
createAppearance(200, 20);
appearance.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12f);
appearance.setColorFill(BaseColor.BLUE);
field.setAppearance(PdfAnnotation.APPEARANCE_DOWN, appearance);
field.setDefaultAppearanceString(appearance);
stamper.getAcroFields().setField("partner", "Click here to show partner's web site");
}
The resulting PDF document is shown without partner field. Please point me to some docs or to mistake at my code.
You are trying to add interactivity to a form. However, you are also throwing away all interactivity by using this line:
stamper.setFormFlattening(true);
You also claim that you are adding an extra field. As far as I can see, that claim is false. You create a field name field (and you create it the hard way; I would expect you to use the TextField class instead). However, I don't see you adding that field anywhere. I miss the following line:
stamper.addAnnotation(field, 1);
Note that this line doesn't make sense:
stamper.getAcroFields().setField("partner", "Click here to show partner's web site");
Why would you create a field first (and possibly add a caption) and then change it immediately afterwards? Why not create the field correctly from the start?
Finally, it seems that you want to create a button that can be clicked by people. Then why are you creating a text field? Wouldn't it make more sense to create a push button?
This is an example of a question to which a machine would respond: Too many errors... Maybe you should consider reading the documentation before trying to fix your code.

TitleAreaDialog - Changing the font for setTitle("abc")

I am extending TitleAreaDialog in my class.
The default font for the title in the title area does not look very nice.
Is it possible to change the font for the title?
I don't need to change the font any where else in the code, just the title text.
I have tried using FontRegistry, as well as StyledText.
But I can not figure out how to assign the new font to setTitle().
FontRegistry fontRegistry = JFaceResources.getFontRegistry();
FontData mainFont = new FontData("Garamond", 18, SWT.NORMAL);
fontRegistry.put("mainFont", new FontData[]{mainFont});
?.setText("Title Text");
?.setFont(fontRegistry.get("mainFont"));
setTitle(?);
I really don't think that's possible. The title String you set via setTitle(String) is displayed in the private field titleLabel. You cannot access this Label when you subclass TitleAreaDialog. Consequently, you cannot apply a Font to it.
So the only possibility I could think of is to create your own MyTitleAreaDialog extends TrayDialog based on the code of the original TitleAreaDialog and set you Font there. You can find the source in your SWT.jar or online.
This is an answer to an old question but it may help someone. You can do it by changing the font in the JFaceResources font registry.
static
{
JFaceResources.getFontRegistry().put(JFaceResources.BANNER_FONT, yourFont.getFontData());
};
I've added this code inside a static block so it only executes once.

Categories