0

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.

user3804769
  • 469
  • 1
  • 8
  • 19
  • You want to display the current style of a control (e.g. Label) to the user and let him provide his own definition which will update the style? Should this edited style be persisted, so it is used the next time the user starts the application? – hotzst Nov 18 '15 at 09:36
  • I want to be able to edit some css properties of some nodes in my program, from my JavaFX program. What you said is partially correct, the important point is being able to edit FX-CSS from Java without having to write a CSS parser from scratch. – user3804769 Nov 18 '15 at 09:47

2 Answers2

0

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()+";");
        });
Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43
  • 1
    Thank you, but this doesn't solve my problem, since I want to be able to directly edit the CSS file. I can't find any Java FX-CSS parsers online... Letting users choose a color isn't a problem, the problem is editing the CSS file since I don't want to write a CSS parser from scratch. – user3804769 Nov 18 '15 at 09:51
  • @user3804769 You can the use `setStyle` and also add your other styles if you wish,check out the edit. – Collins Abitekaniza Nov 18 '15 at 10:01
  • Yes, but this doesn't edit the css *file*... Editing the file is what I'm looking for. I know how to edit the style of a node. Thank you :) – user3804769 Nov 18 '15 at 10:22
  • @user3804769 I don't think you can edit the css file dynamically after loading it to the root,may be first create css file and load it to the root and see what you get. – Collins Abitekaniza Nov 18 '15 at 10:26
  • Yeah, the problem is still how to edit a CSS file without having to write a CSS parser from scratch... Or can you suggest a good CSS parser for Java which doesn't freak out when reading fx-css? I have tried several CSS parsers for Java, can't seem to use any. Zero documentation. – user3804769 Nov 18 '15 at 10:39
  • @user3804769 You're right about the lack of documentation,but you can try this http://grepcode.com/file/repo1.maven.org/maven2/net.java.openjfx.backport/openjfx-78-backport/1.8.0-ea-b96.1/com/sun/javafx/css/parser/CSSParser.java – Collins Abitekaniza Nov 18 '15 at 10:43
  • Thanks, I already found that code... But I have absolutely no idea on how to use it, and reading it really doesn't help.. :( – user3804769 Nov 18 '15 at 10:52
0

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.

user3804769
  • 469
  • 1
  • 8
  • 19