A DESCRIPTION OF THE PROBLEM :
When adding items dynamically in editable combobox, it automatically selects last typed item in editor of combobox, even though the selection is cleared and value is set to null.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create editable combobox with N number of items
2. Type a text in editor of combobox, which is not in list of items of combobox
3. Create a button, which will do as mentioned below.
3.1 Takes text of combobox editor in a string variable
3.2 Clears text of editor of combobox
3.3 Clears selection of combobox
3.4 Sets value of combobox to null
3.5 Adds the typed text of editor of combobox, which is taken in 3.1, which is not in the items list of combobox, to the items list.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Selection and value of combobox should remain null. Because nothing is selected by user. Just the list of items is updated.
ACTUAL -
As soon as 3.5 is performed the combobox automatically selects the last typed item and value is equals to last typed item, whereas previously it was already set to null.
---------- BEGIN SOURCE ----------
package bugreport;
import java.util.ArrayList;
import java.util.List;
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.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Bugreport extends Application
{
@Override
public void start(Stage primaryStage)
{
ComboBox<String> comboBox = new ComboBox<>();
comboBox.setEditable(true);
List<String> l1 = new ArrayList<>();
l1.add("Text1");
l1.add("Text2");
l1.add("Text3");
l1.add("Text4");
l1.add("Text5");
comboBox.getItems().addAll(l1);
Button btn = new Button();
btn.setText("Clear and Add");
btn.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("---------------------------------");
System.out.println(comboBox.getItems().size());
System.out.println(comboBox.getEditor().getText());
System.out.println(comboBox.getSelectionModel().getSelectedIndex());
System.out.println(comboBox.getValue());
System.out.println(comboBox.valueProperty().get());
System.out.println(comboBox.valueProperty().getName());
System.out.println(comboBox.valueProperty().getValue());
System.out.println("---------------------------------");
String editorText = comboBox.getEditor().getText();
if (!l1.contains(editorText)) //Editor contains text which is not already in list
{
//Clear everything in combobox
comboBox.getItems().clear();
comboBox.getSelectionModel().clearSelection();
comboBox.setValue(null);
comboBox.getEditor().clear();
// Add existing items along with a new item
l1.add(editorText);
comboBox.getItems().addAll(l1);
// Combobox automatically sets value as the items are added even though all selection was cleared
System.out.println("---------------------------------");
System.out.println(comboBox.getItems().size());
System.out.println(comboBox.getEditor().getText());
System.out.println(comboBox.getSelectionModel().getSelectedIndex());
System.out.println(comboBox.getValue());
System.out.println(comboBox.valueProperty().get());
System.out.println(comboBox.valueProperty().getName());
System.out.println(comboBox.valueProperty().getValue());
System.out.println("---------------------------------");
}
}
});
HBox root = new HBox();
root.getChildren().add(comboBox);
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Bug Report");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Try to clear selection after adding items
FREQUENCY : always
When adding items dynamically in editable combobox, it automatically selects last typed item in editor of combobox, even though the selection is cleared and value is set to null.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create editable combobox with N number of items
2. Type a text in editor of combobox, which is not in list of items of combobox
3. Create a button, which will do as mentioned below.
3.1 Takes text of combobox editor in a string variable
3.2 Clears text of editor of combobox
3.3 Clears selection of combobox
3.4 Sets value of combobox to null
3.5 Adds the typed text of editor of combobox, which is taken in 3.1, which is not in the items list of combobox, to the items list.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Selection and value of combobox should remain null. Because nothing is selected by user. Just the list of items is updated.
ACTUAL -
As soon as 3.5 is performed the combobox automatically selects the last typed item and value is equals to last typed item, whereas previously it was already set to null.
---------- BEGIN SOURCE ----------
package bugreport;
import java.util.ArrayList;
import java.util.List;
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.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Bugreport extends Application
{
@Override
public void start(Stage primaryStage)
{
ComboBox<String> comboBox = new ComboBox<>();
comboBox.setEditable(true);
List<String> l1 = new ArrayList<>();
l1.add("Text1");
l1.add("Text2");
l1.add("Text3");
l1.add("Text4");
l1.add("Text5");
comboBox.getItems().addAll(l1);
Button btn = new Button();
btn.setText("Clear and Add");
btn.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("---------------------------------");
System.out.println(comboBox.getItems().size());
System.out.println(comboBox.getEditor().getText());
System.out.println(comboBox.getSelectionModel().getSelectedIndex());
System.out.println(comboBox.getValue());
System.out.println(comboBox.valueProperty().get());
System.out.println(comboBox.valueProperty().getName());
System.out.println(comboBox.valueProperty().getValue());
System.out.println("---------------------------------");
String editorText = comboBox.getEditor().getText();
if (!l1.contains(editorText)) //Editor contains text which is not already in list
{
//Clear everything in combobox
comboBox.getItems().clear();
comboBox.getSelectionModel().clearSelection();
comboBox.setValue(null);
comboBox.getEditor().clear();
// Add existing items along with a new item
l1.add(editorText);
comboBox.getItems().addAll(l1);
// Combobox automatically sets value as the items are added even though all selection was cleared
System.out.println("---------------------------------");
System.out.println(comboBox.getItems().size());
System.out.println(comboBox.getEditor().getText());
System.out.println(comboBox.getSelectionModel().getSelectedIndex());
System.out.println(comboBox.getValue());
System.out.println(comboBox.valueProperty().get());
System.out.println(comboBox.valueProperty().getName());
System.out.println(comboBox.valueProperty().getValue());
System.out.println("---------------------------------");
}
}
});
HBox root = new HBox();
root.getChildren().add(comboBox);
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Bug Report");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Try to clear selection after adding items
FREQUENCY : always