函数setOnKeyPressed在DatePicker上不起作用。我想在按下Enter或Down键时将焦点从第一个datepicker(startYear)更改为第二个datepicker(endyear),但它不起作用。 我试过以下代码。请帮帮我...... !!
package datedemo;
import com.sun.javafx.robot.FXRobot;
import com.sun.javafx.robot.FXRobotFactory;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author vikassingh
*/
public class DateDemo extends Application {
DatePicker startYear = new DatePicker();
DatePicker endYear = new DatePicker();
@Override
public void start(Stage primaryStage) {
/* Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
}); */
// Year Start and END
int start_year = 0;
int end_year = 0;
Calendar cal = Calendar.getInstance();
int year = cal.get(cal.YEAR);
int month = cal.get(cal.MONTH) + 1; //zero-based
System.out.println("year = " + year + "\nmonth = " + month);
if (month < 4) {
start_year = year - 1;
end_year = year;
} else {
start_year = year;
end_year = year + 1;
}
String start_year_date = "01/04/" + start_year;
String end_year_date = "31/03/" + end_year;
//Get Start & End Date;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate startDate = LocalDate.parse(start_year_date, formatter);
LocalDate endDate = LocalDate.parse(end_year_date, formatter);
VBox box = new VBox();
VBox.setVgrow(box, Priority.ALWAYS);
Label startYearLabel = new Label("Start Year * :");
box.getChildren().add(startYearLabel);
startYear.setValue(startDate);
startYear.setPromptText("DD/MM/YYYY");
box.getChildren().add(startYear);
Label endYearLabel = new Label("End Year * :");
box.getChildren().add(endYearLabel);
endYear.setValue(endDate);
endYear.setPromptText("DD/MM/YYYY");
box.getChildren().add(endYear);
StackPane root = new StackPane();
root.getChildren().add(box);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Date Demo");
primaryStage.setScene(scene);
primaryStage.show();
// Listner
startYear.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.DOWN) {
endYear.requestFocus();
event.consume();
}
if (event.getCode() == KeyCode.UP) {
// do something
event.consume();
}
}
});
endYear.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.DOWN) {
// do something
event.consume();
}
if (event.getCode() == KeyCode.UP) {
startYear.requestFocus();
event.consume();
}
}
});
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:2)
使用EventFilter,i。即
startYear.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
...
});
endYear.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
...
});
我。即
startYear.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.DOWN) {
endYear.requestFocus();
event.consume();
}
if (event.getCode() == KeyCode.UP) {
// do something
event.consume();
}
});
endYear.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.DOWN) {
// do something
event.consume();
}
if (event.getCode() == KeyCode.UP) {
startYear.requestFocus();
event.consume();
}
});