javafx仅显示日期选择器的按钮,而不显示编辑器 [英] javafx show only the button of a date picker and not the editor
问题描述
我有一个javafx datePicker,我希望完全不显示文本编辑器即可显示。我只希望选择器的按钮可见并处于活动状态。有没有办法做到这一点。当然,我尝试使用datePicker.getEditor()。setVisible(false),但是还有一个令人讨厌的占位符,我想使其不可见。
I have a javafx datePicker and I want to be shown without text editor at all. I want only the button of the picker to be visible and active. Is there any way to do this. Of course I tried the datePicker.getEditor().setVisible(false) but there is stil an annoying place holder which I want to make invisible.
DatePickerView.fxml
DatePickerView.fxml
<HBox>
<children>
<TextField/>
<DatePicker fx:id="datePicker">
<opaqueInsets>
<Insets/>
</opaqueInsets>
</DatePicker>
</children>
</Hbox>
DatePickerController.java
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);
}
推荐答案
您没有描述您要显示的内容而不是textField,我假设您希望按钮填充整个宽度。无论如何,负责创建,配置和布局控件子级的协作者是其外观。因此,基本上,您需要自定义外观,并在此处调整要更改的内容:布局。
As you didn't describe what you want to show instead of the textField, I assume that you want the button to fill the total width. Whatever, the collaborator that's responsible for creating, configuring and layouting the children of a control is its skin. So basically, you'll need a custom skin and tweak what you want to change, here: the layout.
做什么:
- 子类DatePickerSkin
- 在其构造函数中,获取arrowButton的权限(它是私有的,但是您可以通过其样式查找它)
- 覆盖layoutChildren,删除编辑器并更改其宽度按钮来填充宽度
一些原始代码示例(显然还有抛光的余地:)
Some raw code example (obviously there's leeway for polishing :)
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);
}
}
这篇关于javafx仅显示日期选择器的按钮,而不显示编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!