I have a problem. My key binding Ctrl+Tab assigned to inputMap of JDialog doesn't work .
Please, copy-paste this code and type Ctrl+TAB to experience the problem. It should print to console but it doesn't.
public class PopupFilesAccessor extends JDialog {
private static PopupFilesAccessor filesAccessor = new PopupFilesAccessor();
private DefaultListModel<String> filesModel;
private JList<String> files;
public PopupFilesAccessor() {
super(null, "Demo", ModalityType.APPLICATION_MODAL);
super.setUndecorated(true);
super.setAlwaysOnTop(true);
super.setLocationRelativeTo(null);
filesModel = new DefaultListModel<>();
files = new JList<>(filesModel);
files.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
files.setLayoutOrientation(JList.HORIZONTAL_WRAP);
files.setVisibleRowCount(0);
// files.setCellRenderer(new FilesListCellRenderer());
InputMap inputMap = super.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap actionMap = super.getRootPane().getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK), "selectDown");
actionMap.put("selectDown", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
selectNextItem();
}
private void selectNextItem() {
System.out.println("Good! selectNextItem method is running");
}
});
JScrollPane scrollBar = new JScrollPane(files);
super.getContentPane().add(scrollBar);
}
private void prepareToShow() {
List<String> openedFiles = Arrays.asList("item1", "item2", "item3", "item4", "item5", "item6", "item7");
filesModel.clear();
for (int i = 0; i < openedFiles.size(); i++) {
String userFile = openedFiles.get(i);
filesModel.addElement(userFile);
}
files.setSelectedIndex(1);
super.pack();
}
public static void popup() {
filesAccessor.prepareToShow();
filesAccessor.setVisible(true);
filesAccessor.requestFocusInWindow();
}
public static void main(String[] args) {
PopupFilesAccessor.popup();
}
}
Thanks!
It's not a good way but still you can try it using ActionEvent#paramString() if it suits you.
long eventMask = AWTEvent.KEY_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
String paramString = e.paramString();
// System.out.println(paramString);
if (paramString.contains("KEY_RELEASED,keyCode=9,keyText=Tab,keyChar=Tab,modifiers=Ctrl,extModifiers=Ctrl")) {
System.out.println("Ctrl+Tab keys are released");
}
}
}, eventMask);
Note: you can check other combination of param string also.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.