每次在javafx DatePicker中点击周数和/或工作日时,是否可以触发事件?
我的意思是,我可以将actionListener设置为datepicker,当用户点击一天时,它会被触发。但那不是我想要的。我需要知道用户何时点击工作日本身的名称,或者本周的数量。
甚至可能吗?提前谢谢!
答案 0 :(得分:1)
这应该让你朝着正确的方向前进。这演示了如何将onMouseClicked
事件处理程序添加到Day of the Week
节点。它还显示了如何为其他节点找出CSS
。
密钥号码:
for (Node node : popupContent.lookupAll(".day-name-cell")) {
DateCell tempDateCell = (DateCell) node;
tempDateCell.setOnMouseClicked((event) -> {
System.out.println("You clicked: " + tempDateCell.getText());
});
}
完整代码:
import com.sun.javafx.scene.control.skin.DatePickerSkin;
import java.time.LocalDate;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication147 extends Application
{
@Override
public void start(Stage primaryStage)
{
DatePicker datePicker = new DatePicker(LocalDate.now());
DatePickerSkin datePickerSkin = new DatePickerSkin(datePicker);
Node popupContent = datePickerSkin.getPopupContent();
//To see day of the week nodes
for (Node node : popupContent.lookupAll(".day-name-cell")) {
node.setOnMouseClicked((event) -> {
DateCell dateCell = (DateCell) node;
System.out.println("You clicked: " + dateCell.getText());
});
}
//To see all nodes. Used to find out how to look up certain nodes
System.out.println("\nAll nodes:");
for (Node node : popupContent.lookupAll("*")) {
System.out.println("\t" + node);
}
StackPane root = new StackPane(popupContent);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
输出:
All nodes:
DatePickerContent@58c2582f[styleClass=date-picker-popup]
BorderPane@9a2c4e3[styleClass=month-year-pane]
HBox@6237f3e7[styleClass=spinner]
Button@5468474f[styleClass=button left-button]''
Label@4a04f04f[styleClass=label spinner-label]'March'
Button@68b04c02[styleClass=button right-button]''
HBox@6e61c6ba[styleClass=spinner]
Button@d7d5b3e[styleClass=button left-button]''
Label@508829bd[styleClass=label spinner-label]'2018'
Button@3837351b[styleClass=button right-button]''
Grid hgap=-1.0, vgap=-1.0, alignment=TOP_LEFT
DateCell@6a1b4345[styleClass=cell date-cell day-name-cell]'Sun'
DateCell@76997e3d[styleClass=cell date-cell day-name-cell]'Mon'
DateCell@c263fb4[styleClass=cell date-cell day-name-cell]'Tue'
DateCell@29bdab5e[styleClass=cell date-cell day-name-cell]'Wed'
DateCell@190cd447[styleClass=cell date-cell day-name-cell]'Thu'
DateCell@245929cd[styleClass=cell date-cell day-name-cell]'Fri'
DateCell@193f696d[styleClass=cell date-cell day-name-cell]'Sat'
DateCell@5d9cc64f[styleClass=cell date-cell day-cell previous-month]'25'
DateCell@224d9f57[styleClass=cell date-cell day-cell previous-month]'26'
DateCell@4fb7dd92[styleClass=cell date-cell day-cell previous-month]'27'
DateCell@2da1a108[styleClass=cell date-cell day-cell previous-month]'28'
DateCell@1634b44d[styleClass=cell date-cell day-cell]'1'
DateCell@584d26fb[styleClass=cell date-cell day-cell]'2'
DateCell@29e72b2d[styleClass=cell date-cell day-cell]'3'
DateCell@96e66f8[styleClass=cell date-cell day-cell]'4'
DateCell@3a4b2d44[styleClass=cell date-cell day-cell]'5'
DateCell@b80be08[styleClass=cell date-cell day-cell]'6'
DateCell@1b2e4216[styleClass=cell date-cell day-cell]'7'
DateCell@610dbe2d[styleClass=cell date-cell day-cell]'8'
DateCell@2cf9e45[styleClass=cell date-cell day-cell]'9'
DateCell@cb100ea[styleClass=cell date-cell day-cell]'10'
DateCell@65e6dd98[styleClass=cell date-cell day-cell]'11'
DateCell@6916ebdf[styleClass=cell date-cell day-cell]'12'
DateCell@17d57d0e[styleClass=cell date-cell day-cell]'13'
DateCell@1367994e[styleClass=cell date-cell day-cell]'14'
DateCell@40be8ca7[styleClass=cell date-cell day-cell]'15'
DateCell@1ea8cf7c[styleClass=cell date-cell day-cell]'16'
DateCell@2229ecaa[styleClass=cell date-cell day-cell]'17'
DateCell@dabac48[styleClass=cell date-cell day-cell]'18'
DateCell@3944c2ee[styleClass=cell date-cell day-cell]'19'
DateCell@614f3995[styleClass=cell date-cell day-cell]'20'
DateCell@c6ed5aa[styleClass=cell date-cell day-cell]'21'
DateCell@5cf7e183[styleClass=cell date-cell day-cell]'22'
DateCell@43bb1b41[styleClass=cell date-cell day-cell]'23'
DateCell@5ba01132[styleClass=cell date-cell day-cell]'24'
DateCell@21995c69[styleClass=cell date-cell day-cell]'25'
DateCell@42014a20[styleClass=cell date-cell day-cell]'26'
DateCell@597b2032[styleClass=cell date-cell day-cell]'27'
DateCell@20993ae[styleClass=cell date-cell day-cell today selected]'28'
DateCell@6918ed92[styleClass=cell date-cell day-cell]'29'
DateCell@2180eadb[styleClass=cell date-cell day-cell]'30'
DateCell@21fa2469[styleClass=cell date-cell day-cell]'31'
DateCell@6fa0571b[styleClass=cell date-cell day-cell next-month]'1'
DateCell@41fa4300[styleClass=cell date-cell day-cell next-month]'2'
DateCell@670abfd6[styleClass=cell date-cell day-cell next-month]'3'
DateCell@17231632[styleClass=cell date-cell day-cell next-month]'4'
DateCell@7ad68342[styleClass=cell date-cell day-cell next-month]'5'
DateCell@69273d5b[styleClass=cell date-cell day-cell next-month]'6'
DateCell@5b61dba6[styleClass=cell date-cell day-cell next-month]'7'