问题描述
我想创建一个表,我想在其中配置热键快捷键。
I want to create table in which I want to configure hot key shortcuts.
我有这个简单的表:
public static final String Column1MapKey = "A";
public static final String Column2MapKey = "B";
private ObservableList<Map> generateDataInMap() {
int max = 110;
ObservableList<Map> allData = FXCollections.observableArrayList();
for (int i = 1; i < max; i++) {
Map<String, String> dataRow = new HashMap<>();
String value1 = "A" + i;
String value2 = "B" + i;
dataRow.put(Column1MapKey, value1);
dataRow.put(Column2MapKey, value2);
allData.add(dataRow);
}
return allData;
}
public TabPane hotKeysContent(){
TableColumn<Map, String> firstDataColumn = new TableColumn<>("Actions");
TableColumn<Map, String> secondDataColumn = new TableColumn<>("Shortcut");
firstDataColumn.setCellValueFactory(new MapValueFactory(Column1MapKey));
firstDataColumn.setMinWidth(230);
secondDataColumn.setCellValueFactory(new MapValueFactory(Column2MapKey));
secondDataColumn.setMinWidth(230);
TableView table_view = new TableView<>(generateDataInMap());
table_view.setPadding(new Insets(5, 5, 5, 5));
table_view.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); // Autoresize when window size is changed
table_view.setEditable(true);
table_view.getSelectionModel().setCellSelectionEnabled(true);
table_view.getColumns().setAll(firstDataColumn, secondDataColumn);
Callback<TableColumn<Map, String>, TableCell<Map, String>>
cellFactoryForMap = new Callback<TableColumn<Map, String>,
TableCell<Map, String>>() {
@Override
public TableCell call(TableColumn p) {
return new TextFieldTableCell(new StringConverter() {
@Override
public String toString(Object t) {
return t.toString();
}
@Override
public Object fromString(String string) {
return string;
}
});
}
};
firstDataColumn.setCellFactory(cellFactoryForMap);
secondDataColumn.setCellFactory(cellFactoryForMap);
return null;
}
我想点击第二列中的一行来获得组合按键,我将按,然后使用这些键激活键盘快捷键。任何例子都会有所帮助。
I want when I click on a row into the second column to get the combination of keys which I will press and later to use these keys to activate keyboard shortcuts. Any example will be helpful.
带有命令的PS表:
public static final String Column1MapKey = "A";
public static final String Column2MapKey = "B";
private ObservableList<Map> generateDataInMap() {
int max = 110;
ObservableList<Map> allData = FXCollections.observableArrayList();
for (int i = 1; i < max; i++) {
Map<String, String> dataRow = new HashMap<>();
String value1 = "A" + i;
String value2 = "B" + i;
dataRow.put(Column1MapKey, value1);
dataRow.put(Column2MapKey, value2);
allData.add(dataRow);
}
return allData;
}
public TabPane hotKeysContent(){
TabPane tabPane = new TabPane();
//tabPane.setStyle("-fx-font-size: 13pt;"); // Set size of the tab name
Tab tabA = new Tab();
Label tabALabel = new Label("Shortcuts");
//tabALabel.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
tabA.setGraphic(tabALabel);
tabA.setClosable(false); // da se mahne opciqta da se zatvarq tab
TableColumn<Map, String> firstDataColumn = new TableColumn<>("Actions");
TableColumn<Map, String> secondDataColumn = new TableColumn<>("Shortcut");
firstDataColumn.setCellValueFactory(new MapValueFactory(Column1MapKey));
firstDataColumn.setMinWidth(230);
secondDataColumn.setCellValueFactory(new MapValueFactory(Column2MapKey));
secondDataColumn.setMinWidth(230);
TableView table_view = new TableView<>(generateDataInMap());
table_view.setPadding(new Insets(5, 5, 5, 5));
table_view.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); // Autoresize when window size is changed
table_view.setEditable(true);
table_view.getSelectionModel().setCellSelectionEnabled(true);
table_view.getColumns().setAll(firstDataColumn, secondDataColumn);
Callback<TableColumn<Map, String>, TableCell<Map, String>>
cellFactoryForMap = new Callback<TableColumn<Map, String>,
TableCell<Map, String>>() {
@Override
public TableCell call(TableColumn p) {
return new TextFieldTableCell(new StringConverter() {
@Override
public String toString(Object t) {
return t.toString();
}
@Override
public Object fromString(String string) {
return string;
}
});
}
};
firstDataColumn.setCellFactory(cellFactoryForMap);
secondDataColumn.setCellFactory(cellFactoryForMap);
tabA.setContent(table_view);
tabPane.getTabs().add(tabA);
return tabPane;
}
推荐答案
下面是SSCCE,虽然没有tableView没有列或单元格。但逻辑是相似的。检查它是否有线索,并根据您的需要编写自己的代码:
Below is a SSCCE, though there is no tableView no column or cell. However the logic is similar. Examine it for clues and write your own code according to your needs:
public class ShortCutDemo extends Application {
private KeyEvent shortcutKeyEvent;
private EventHandler selectedEventHandler;
private List<EventHandler> eventHandlers;
private HBox root;
@Override
public void start(Stage primaryStage) {
root = new HBox(10);
root.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
// Do not filter for TextFields
if (event.getTarget() instanceof TextField) {
return;
}
if (isKeyEventsAreEqual(event, shortcutKeyEvent)) {
// then apply shortcut event
selectedEventHandler.handle(null);
event.consume();
}
}
});
eventHandlers = new ArrayList<EventHandler>();
eventHandlers.add(new EventHandler() {
@Override
public void handle(Event event) {
root.setStyle("-fx-background-color: lightgray");
}
});
eventHandlers.add(new EventHandler() {
@Override
public void handle(Event event) {
root.setSpacing(50);
}
});
ChoiceBox cb = new ChoiceBox();
cb.getItems().addAll("HBox background = gray", "HBox spacing = 50");
cb.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
selectedEventHandler = eventHandlers.get(newValue.intValue());
}
});
cb.getSelectionModel().selectFirst(); // default value
final TextField textField = new TextField();
textField.setEditable(false);
textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.TAB) {
// Platform.runLater(new Runnable() {
// @Override
// public void run() {
// root.requestFocus();
// }
// });
} else {
// Clear the previous text
textField.setText("");
// Process only desired key types
if (event.getCode().isLetterKey()
|| event.getCode().isDigitKey()
|| event.getCode().isFunctionKey()) {
String shortcut = event.getCode().getName();
if (event.isAltDown()) {
shortcut = "Alt + " + shortcut;
}
if (event.isControlDown()) {
shortcut = "Ctrl + " + shortcut;
}
if (event.isShiftDown()) {
shortcut = "Shift + " + shortcut;
}
textField.setText(shortcut);
shortcutKeyEvent = event;
} else {
shortcutKeyEvent = null;
}
}
}
});
Button button = new Button("Reset");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
textField.setText("");
root.setSpacing(10);
root.setStyle("-fx-background-color: white");
shortcutKeyEvent = null;
}
});
root.getChildren().addAll(new Label("Define a shortcut for "), cb, textField, button);
Scene scene = new Scene(root, 900, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
private boolean isKeyEventsAreEqual(KeyEvent event1, KeyEvent event2) {
return event1 != null
&& event2 != null
&& event1.getCode() == event2.getCode()
&& event1.isAltDown() == event2.isAltDown()
&& event1.isControlDown() == event2.isControlDown()
&& event1.isShiftDown() == event2.isShiftDown();
}
public static void main(String[] args) {
launch(args);
}
}
如何运作?
1)从选择框中选择一个动作(事件)类型。
2)聚焦到Textfield并输入所需的快捷方式, + 例如。
3)按从TextField中聚焦。
4)按快捷键( + 在这种情况下)再次看到事件在起作用。
5)按重置按钮确定重置状态。
How it works?
1) Select an action (event) type from choicebox.
2) Focus to Textfield and type the desired shortcut, + for example.
3) Press to focus out from TextField.
4) Press the shortcut (+ in this case) again to see the event in action.
5) Press "Reset" button to reset the state surely.
进一步的改进应该是定义一个模型类,它有ShortCut键,用于Ctrl,Alt和Shift的布尔值,用于处理的事件处理程序并适当地覆盖 equals(...)
方法。
The further improvement should be defining a model class, that have ShortCut key, booleans for Ctrl, Alt and Shift, eventhandler to process and appropriately overriden equals(...)
method.
这篇关于如何检测密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!