Skip to content
Permalink
Browse files
8264157: Items of non-editable ComboBox cannot be selected using up/d…
…own keys

Reviewed-by: fastegal, kcr
  • Loading branch information
arapte committed May 6, 2021
1 parent 599ca1e commit 4b6c587faacdc1f5f00df6e4bf5ad57c3a48723f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -26,9 +26,55 @@
package com.sun.javafx.scene.control.behavior;

import javafx.scene.control.ComboBox;
import javafx.scene.control.ComboBoxBase;
import javafx.scene.control.SelectionModel;
import com.sun.javafx.scene.control.inputmap.InputMap;

import static javafx.scene.input.KeyCode.DOWN;
import static javafx.scene.input.KeyCode.UP;

public class ComboBoxListViewBehavior<T> extends ComboBoxBaseBehavior<T> {

/***************************************************************************
* *
* Constructors *
* *
**************************************************************************/

/**
*
*/
public ComboBoxListViewBehavior(final ComboBox<T> comboBox) {
super(comboBox);

// Add these bindings as a child input map, so they take precedence
InputMap<ComboBoxBase<T>> comboBoxListViewInputMap = new InputMap<>(comboBox);
comboBoxListViewInputMap.getMappings().addAll(
new InputMap.KeyMapping(UP, e -> selectPrevious()),
new InputMap.KeyMapping(DOWN, e -> selectNext())
);
addDefaultChildMap(getInputMap(), comboBoxListViewInputMap);
}

/***************************************************************************
* *
* Key event handling *
* *
**************************************************************************/

private ComboBox<T> getComboBox() {
return (ComboBox<T>) getNode();
}

private void selectPrevious() {
SelectionModel<T> sm = getComboBox().getSelectionModel();
if (sm == null) return;
sm.selectPrevious();
}

private void selectNext() {
SelectionModel<T> sm = getComboBox().getSelectionModel();
if (sm == null) return;
sm.selectNext();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -1308,6 +1308,27 @@ public void defaultConverterCanHandleIncorrectType_2() {
assertTrue(cb.isShowing());
}

@Test public void testEditorKeyInputsWhenPopupIsNotShowing() {
final ComboBox<String> cb = new ComboBox<>(FXCollections.observableArrayList("a", "b"));
sl = new StageLoader(cb);
KeyEventFirer keyboard = new KeyEventFirer(cb);
cb.requestFocus();

// Sanity
assertFalse(cb.isShowing());
assertEquals(null, cb.getValue());

// Test DOWN and UP key
keyboard.doKeyPress(KeyCode.DOWN);
assertEquals("a", cb.getValue());
keyboard.doKeyPress(KeyCode.DOWN);
assertEquals("b", cb.getValue());
keyboard.doKeyPress(KeyCode.UP);
assertEquals("a", cb.getValue());
keyboard.doKeyPress(KeyCode.UP);
assertEquals("a", cb.getValue());
}

@Test public void testEditorKeyInputsWhenPopupIsShowing() {
final ComboBox<String> cb = new ComboBox<>(FXCollections.observableArrayList("a", "b", "c"));
cb.setEditable(true);

1 comment on commit 4b6c587

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 4b6c587 May 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.