我有一个javafx datePicker,我希望在没有文本编辑器的情况下显示。我只希望选择器的按钮可见并且活动。有没有办法做到这一点。当然我尝试了datePicker.getEditor()。setVisible(false)但是有一个讨厌的占位符我想让它看不见。
DatePickerView.fxml
<HBox>
<children>
<TextField/>
<DatePicker fx:id="datePicker">
<opaqueInsets>
<Insets/>
</opaqueInsets>
</DatePicker>
</children>
</Hbox>
DatePickerController.java
public class DatePickerComtroller implements Initializable{
@FXML
DatePicker datePicker;
@FXML
HBox container;
public DatePickerComtroller(){
}
public initialize(URL location, ResourceBundle resources){
datePicker.getEditor().setVisible(false);
}
答案 0 :(得分:2)
由于您没有描述要显示的内容而不是textField,我假设您希望按钮填充总宽度。无论如何,负责创建,配置和布局控件子项的合作者就是它的皮肤。所以基本上,你需要一个自定义皮肤并调整你想要改变的东西,在这里:布局。
怎么做:
一些原始代码示例(显然有抛光的余地:)
public class MyPickerSkin extends DatePickerSkin {
StackPane arrowButtonAlias;
public MyPickerSkin(DatePicker control) {
super(control);
arrowButtonAlias = (StackPane) control.lookup(".arrow-button");
}
@Override
protected void layoutChildren(double x, double y, double w, double h) {
super.layoutChildren(x, y, w, h);
// seems the removal must happen after super's layout (see OPs comment)
getChildren().remove(getEditor());
arrowButtonAlias.resizeRelocate(x, y, w, h);
}
}