JavaFX - edit or parse FX-CSS file programmatically - java

I am using JavaFX 8u60. I want to give my users the chance to edit a CSS file for a pane in my program, without using an external editor.
For example, the user clicks on a Label, and a dialog to select the color is shown. After the user selects the color, the color is actually written in the CSS file, in the appropriate line...
Are there CSS parsers for JavaFX?
I can't show you any Java code because I'm not sure this can be done.
.table-view .column-header .label{
-fx-font: 18 GatwickSans;
-fx-text-fill: red; //<--- user shall be able to edit this line from my program
-fx-alignment: TOP_LEFT;
}
edit: to clarify, I want to be able to edit a FX-CSS file from Java.

You can use color picker, Try this example
Hbox layout =new HBox(10);
ColorPicker colorPicker = new ColorPicker();
colorPicker.setValue(Color.RED);//Red is the default shown at first
Label label =new Label("Your Text");
layout.getChildren().addAll(label,colorPicker);
//Then
colorPicker.setOnAction(event->{
label.setFill(colorPicker.getValue());
});
Also for css
colorPicker.setOnAction(event->{
label.setStyle("-fx-text-fill: "+colorPicker.getValue()+";");
});

I have used CSSParser:
http://sourceforge.net/projects/cssparser/
It's sufficiently generic and it works a bit like the DOM XML parser. It reads the CSS file and maps it in memory, allowing you to read the single selectors, properties and values, and edit them. Check the discussion on Sourceforge to have some examples, since it lacks documentation.

Related

Style the background of a Vaadin 14 Dialog

I want to change the background-color of a Vaadin 14 Dialog. I did not get it how to do it. There is no method for it. The id is "overlay" and the color is #3b85e6.
How to do that? I only managed to add styles to the content, but i want to change the background.
In your Java code, your view class, add import for styles.
#CssImport(value = "./styles/my-dialog.css", themeFor = "vaadin-dialog-overlay")
And create file "frontend/styles/my-dialog.css", whos content is
:host {
background: #3b85e6;
}

Vaadin 14 grid multi-line cells

I have quite long Strings in one column and I want to display them as multi-line cells in my grid.
I'm using Vaadin 14 + Java and I tried to set CSS-Style Class for the specific column:
Java-Code:
#CssImport("./styles/shared-styles.css")
public class RisikoGrid extends Grid<RisikoEntity> {
public RisikoGrid() {
setSizeFull();
// add the column to the grid
addColumn(Entity::getAttribute).setHeader("MyCol")
.setFlexGrow(10).setSortable(true).setKey("mycolumn");
// set CSS style class for the specific column
this.getColumnByKey("mycolumn").setClassNameGenerator(item -> {return "grid-mycol";});
}
}
CSS (shared-styles.css)
.grid-mycol{
background: red;
white-space: normal;
word-wrap: break-word;
}
While I do see the class-name when I use the inspector in my webbrowser (chrome), the css is not applied.
What do I need to change to make it work?
Edit: this is how my styles look like - and I can't even see the background:red for instance:
You can use the built-in wrap-cell-content theme variant to allow text to wrap inside the cells.
grid.addThemeVariants(GridVariant.LUMO_WRAP_CELL_CONTENT);
See the last example on https://vaadin.com/components/vaadin-grid/html-examples/grid-theme-demos – the texts there are not that long, but if you make your browsers window narrow enough you’ll see some of the addresses wrap on two lines.
I looked into why my css settings didn't show up in the inspector and the solution was a minimal change from:
#CssImport("./styles/shared-styles.css")
to
#CssImport(value = "./styles/shared-styles.css", themeFor = "vaadin-grid")
I'm guessing the component scope of the grid differs from the global CSS.
It works now.

Adding buttons with multi line text but both lines having center paragraph alignment

I'm trying to create a button that has two lines of text in it. This is easy enough to do using the <br/> tags in <html> however the larger line is center aligned but the smaller line is floating left:
Below is my code for generating this button (I've wrapped the text so it doesn't look ugly). Is it possible to get the first line "View Config File" to appear centrally aligned, keeping the central alignment for the second line?
JButton viewConfigFile = new JButton("<html>View Config File" +
"<br/>Be careful of any changes made</html>");
Original suggestion was to use the center tag to wrap the text but Pete pointed out this tag is obsolete and deprecated in HTML 4 (so I don't recommend to use this).
JButton button = new JButton("<html><center>View Config File"
+ "<br/>Be careful of any changes made</center></html>");
Instead you can use the CSS text-align property:
JButton button = new JButton("<html><div text-align:center>View Config File" + "<br/>Be careful of any changes made</div></html>");
See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/center

Vaadin: Dynamically change a textfield's background color

How does one dynamically change a textfield's visual properties, say background color?
An example might be during a validation to modify the background color dependent on the value entered.
In your war (this will be inside WebContent if you are using ecilpse) have a file in this path VAADIN/themes/customrunno/styles.css (asuming you are using runno as your theme)
With this content:
#import url(../runo/styles.css);
.my_bg_style {
background: #99EE6B;
}
In your init method:
setTheme("customruno"); // same as the folder folder under VAADIN/themes
change your textfield background:
textField.addStyleName("my_bg_style"); // same as in styles.css
to remove the style just do:
textField.removeStyleName("my_bg_style");
Here is the sample
.v-textfield-dashing
{
border: thin dashed;
background: white; /* Has shading image by default */
}
Detailed information you will get on this link.
https://vaadin.com/book/-/page/components.textfield.html
And if you want to change textfield color dynamically then you will get content on below link
https://vaadin.com/wiki/-/wiki/Main/Dynamically%20injecting%20CSS

How to set colour of text from a certain point forward

I'm building a small conversation agent, where the text looks like follows:
I would like to set the System's text to always be red. The text is all placed in a JTextPane.
How can I accomplish this? I have tried doing the following:
agentTextPane.setForeground(Color.red); after the system's text is added, and then switching back to black, however that changes all the text in the JTextPane.
This is how the system's text is added:
//'output' is a stringBuilder
output.append("\nSystem: ").append(tempOutput).append("\n");
agentTextPane.setText(output.toString());
As shown here, you can define an attribute set representing a desired style. For example,
StyledDocument doc = (StyledDocument) jtp.getDocument();
SimpleAttributeSet system = new SimpleAttributeSet();
StyleConstants.setFontFamily(system, "Serif");
StyleConstants.setForeground(system, Color.red);
doc.insertString(doc.getLength(), "...", system);
The styles can be progressive, as shown here.
See Text Component Features for more examples.
You may want to use HTML tags to format your Strings in terms of colour. The following reference may be useful. setting JTextPane to content type HTML and using string builders
output.append("<font color=\"red\">");
output.append("\nSystem: ").append(tempOutput).append("\n");
output.append("</font>");

Categories