This discussion is archived
1 Reply Latest reply: Jan 13, 2013 2:05 PM by shakir.gusaroff RSS

ComboBox issue

edward17 Newbie
Currently Being Moderated
So I am sure Jonathan groans when he sees my name pop up, but I found another ComboBox issue and I am looking for help finding a workaround.

(I opened a JIRA - http://javafx-jira.kenai.com/browse/RT-27654)

Seems that calling clearSelecion() on a ComboBox disallows selecting the same item that was selected when the clearSelection() was called. In my sample code, if you

1) select 'a' - 'a' displays
2) click reset button - 'a' disappears
3) select 'a' - 'a' does NOT appear and it should

The issue is major when the combobox only has a singe item
package javafxapplication2; 

import java.net.URL; 
import java.util.ArrayList; 
import java.util.ResourceBundle; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.ComboBox; 

/** 
 * 
 * @author user 
 */ 
public class SampleController implements Initializable { 
     
    @FXML 
    private ComboBox<MyObject> cb; 
        private ObservableList<MyObject> myList = FXCollections.observableList(new ArrayList<MyObject>()); 
     
    @FXML 
    private void handleButtonAction(ActionEvent event) { 
        cb.getSelectionModel().clearSelection(); 
    } 
     
    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
        MyObject a = new MyObject("a", "A"); 
        MyObject b = new MyObject("b", "B"); 
        MyObject c = new MyObject("c", "C"); 
        myList.add(a); 
        myList.add(b); 
        myList.add(c); 
        cb.setItems(myList); 
    } 
     
    class MyObject { 
        String key; 
        String Value; 

        public MyObject(String key, String Value) { 
            this.key = key; 
            this.Value = Value; 
        } 
        @Override 
        public String toString() { 
            return key; 
        } 
    } 
} 
<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.collections.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication2.SampleController"> 
  <children> 
    <ComboBox fx:id="cb" layoutX="82.0" layoutY="78.0" prefWidth="160.0"> 
      <items> 

      </items> 
    </ComboBox> 
    <Button onAction="#handleButtonAction" layoutX="130.0" layoutY="137.0" mnemonicParsing="false" text="Reset" /> 
  </children> 
</AnchorPane> 
  • 1. Re: ComboBox issue
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. Here is a workaround.
    //Modified  initialize method.  Added a null object to the list:
      @Override 
        public void initialize(URL url, ResourceBundle rb) { 
            MyObject a = new MyObject("a", "A"); 
            MyObject b = new MyObject("b", "B"); 
            MyObject c = new MyObject("c", "C"); 
            myList.add(a); 
            myList.add(b); 
            myList.add(c); 
            myList.add(null);    
            cb.setItems(myList); 
        } 
          
    Modified handleButtonAction(ActionEvent event) method:
    private void handleButtonAction(ActionEvent event) { 
          
             cb.getSelectionModel().selectLast();
             cb.getSelectionModel().clearSelection();
             
        }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points