且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

javafx-更改插入符号颜色的最简单方法

更新时间:2023-11-28 21:16:10

经过一番尝试&错误,我通过以下方式解决了问题:

After a bit of try & error, I solved the problem in the following way:

我收集了所有 TextField 和其中包含 TextField 的控件(例如 ComboBox DatePicker 和依此类推)(根据 TitledPane ScrollPane SplitPane TabPane 递归),因为它们没有不会在 getChildren()中发布其子代,因此必须调用各个类的 getContent()方法并对其进行扫描).

I gathered all TextFields and controls that have TextFields in them (like ComboBox, DatePicker and so on) inside a container recursively (in deference of TitledPane, ScrollPane, SplitPane and TabPane, because they don't publish their children in getChildren(), so one has to call the getContent() method of the individual classes and scan through it).

在拥有所有 TextField 控件之后,我遍历了它们,并使用以下代码更改了它们的 Skin :

After I had all the TextField controls, I looped over them and changed their Skin with the following code:

public class MyTextFieldSkin extends TextFieldSkin {
   public MyTextFieldSkin(TextField tf)
   {
      super(tf);
      ReadOnlyObjectWrapper<Color> color = new ReadOnlyObjectWrapper<>(Color.RED);
      caretPath.strokeProperty().bind(color);          
   }
}

那我只需要打电话

textfield.setSkin(new MyTextFieldSkin(textfield));

就是这样.

欢呼