[REVIEW REQUEST] Add text property to ComboBox

Tadashi Ohmura omurata at ga2.so-net.ne.jp
Wed Mar 14 06:12:13 PDT 2012


Shura,

The object in the input field of editable combobox seems to be String in 
spite of the type of combobox.

I run the following sample code ComboBox01E
//===============================================================
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ComboBox01E extends Application {
   // Integer ComboBox

   public void start(Stage stage) throws Exception {
     HBox root = new HBox();
     Scene scene = new Scene( root, 240, 240 );
     stage.setScene( scene );
     //
     ComboBox<Integer> comboBox = new ComboBox<Integer>();
     comboBox.getItems().addAll( 111, 222, 333, 444, 555, 666, 777, 888, 
999 );
     comboBox.setEditable(true);
     comboBox.valueProperty().addListener( selectedValue );
     //
     root.getChildren().add( comboBox );
     stage.setTitle("ComboBox01E");
     stage.show();
   }
   //
   ChangeListener<Number> selectedValue = new ChangeListener<Number>() {
     public void changed( ObservableValue<? extends Number> value, 
Number oldVal, Number newVal ) {
       System.out.println( " oldVal="+ oldVal +" newVal="+ newVal );
     }
   };
   //
   public static void main(String[] args) {
     launch(args);
   }
}
//===============================================================

The type of this editable combobox is Integer
When I enter the numeric characters such as 111 then hit Enter key
the exception rises:

java.lang.ClassCastException: java.lang.String cannot be cast to 
java.lang.Number
     at ComboBox01E$1.changed(ComboBox01E.java:1)
     at 
com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:367)
     at 
com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
     at 
javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:123)
     at 
javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:130)
     at 
javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
     at javafx.scene.control.ComboBoxBase$1.set(ComboBoxBase.java:86)
     at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:94)

Best regards
   Tadashi Ohmura

(2012/03/14 20:28), Alexandre (Shura) Iline wrote:
> Jonathan,
>
> Judging from the javadoc, combobox component could handle different 
> types - not only String. I could redefine the skin, I assume, and have 
> any editor within it, so, there may not be a text field to get a text 
> from in the first place.
>
> Should the "text" property be of a type T and named somehow else?
>
> Shura.
>
> On 03/14/2012 04:53 AM, Jonathan Giles wrote:
>> Hi all (yet again),
>>
>> A quick one this time: http://javafx-jira.kenai.com/browse/RT-19589
>>
>> I'm wanting to add a text property to ComboBox, to allow for people to
>> easily set the text in the ComboBox without committing it to being a
>> value. More importantly, this also allows for developers to extract the
>> content of the ComboBox TextField without having to have end-users press
>> the Enter key to commit the text into the value property.
>>
>> Without this end users who want to support Enter-less committing of
>> content have to reach into the ComboBox skin, which is not particularly
>> pleasant.
>>
>> Any thoughts?
>>
>
>



More information about the openjfx-dev mailing list