JavaFX - edit or parse FX-CSS file programmatically

两盒软妹~` 提交于 2019-12-12 05:25:59

问题


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.


回答1:


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()+";");
        });



回答2:


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.



来源:https://stackoverflow.com/questions/33776022/javafx-edit-or-parse-fx-css-file-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!