The ComboBox is created using a combination of Label and ListView. The ListView instance is dynamically inserted into content of Scene so that it appears on top of all existing content. Its deleted from Scene on making list invisible.

The entire logic of control is implemented in Skin. Behavior class can be implemented to handle keyboard events. The look can be easily customized.

Like this:

Related

27 Responses to JavaFX – ComboBox

This is very very useful control box. I was looking for similar thing in my PodcastFeedViewer Sample.
We can improve this
a) KeyListener ( onKeyPressed ) Press J – Java should be selected etc.
b)Making Editable Combo Box.
etc.

Works great for me to display lists of items!
But I can’t figure out how to USE it. For e.g. if I want to pass selectedItem to some other function, whenever selected item is changed (onKeyPressed for e.g.), how would you be able to do that? I keep getting into cyclic reference error!
e.g.
var itemList = ComboBox {
items: [
"Please select an item…"
]
onKeyPressed: function(evt : KeyEvent) {
System.out.println("Selected {itemList.selectedItem}");
}

I started to use your Main application. It looks good. However after a few sets of the controls I noticed this stack trace. It indicates that somewhere in your code you are assigning to a value that is bound. As I am sure you know, this problem can be fixed by assigning to a variable on the RHS of a bind statement (rather than on the LHS).
com.sun.javafx.runtime.AssignToBoundException: Cannot assign to bound variable
at com.sun.javafx.runtime.location.ObjectVariable.set(ObjectVariable.java:113)
at javafx.scene.control.TextInputControl.set$text(TextInputControl.fx:54)
at javafx.scene.control.TextBox.commit(TextBox.fx:465)
at com.sun.javafx.scene.control.caspian.TextBoxSkin$TextBoxBehavior.focusChanged(TextBoxSkin.fx:1027)
at com.sun.javafx.scene.control.caspian.TextBoxSkin$_SBECL.onChange(TextBoxSkin.fx:1016)
at com.sun.javafx.runtime.location.BooleanVariable.notifyListeners(BooleanVariable.java:146)
at com.sun.javafx.runtime.location.BooleanVariable.replaceValue(BooleanVariable.java:94)
at com.sun.javafx.runtime.location.AbstractBindingExpression.pushValue(AbstractBindingExpression.java:92)
at com.sun.javafx.runtime.location.BooleanVariable$1.compute(BooleanVariable.java:104)
at com.sun.javafx.runtime.location.AbstractVariable.update(AbstractVariable.java:128)
at com.sun.javafx.runtime.location.AbstractVariable.invalidate(AbstractVariable.java:142)
at com.sun.javafx.runtime.location.AbstractLocation$2.onAction(AbstractLocation.java:235)

Hi Rakesh,
I am getting the following exception when I added your combobox to my desktop application. I get the exception when I click on the combobox. Any idea how to fix this?
Thanks,
Hari
Exception in thread "AWT-EventQueue-0" com.sun.javafx.runtime.AssignToBoundException: Cannot mutate bound sequence
at com.sun.javafx.runtime.location.SequenceVariable.ensureNotBound(SequenceVariable.java:218)
at com.sun.javafx.runtime.location.SequenceVariable.delete(SequenceVariable.java:389)
at com.sun.javafx.runtime.location.SequenceVariable.deleteValue(SequenceVariable.java:430)
at combobox.ComboBoxSkin.show(ComboBoxSkin.fx:306)
at combobox.ComboBoxSkin.show(ComboBoxSkin.fx:306)
at combobox.ComboBoxSkin$5.lambda(ComboBoxSkin.fx:268)
at combobox.ComboBoxSkin$5.lambda(ComboBoxSkin.fx:268)
at combobox.ComboBoxSkin$5.invoke(ComboBoxSkin.fx:264)
at combobox.ComboBoxSkin$5.invoke(ComboBoxSkin.fx:264)
at combobox.ComboBoxSkin$5.invoke(ComboBoxSkin.fx:264)
at combobox.ComboBoxSkin$5.invoke(ComboBoxSkin.fx:264)
at javafx.scene.Node.impl_processMouseEvent(Node.fx:1677)
at javafx.scene.Node.preprocessMouseEvent(Node.fx:1705)
at javafx.scene.Scene$MouseHandler.process(Scene.fx:665)
at javafx.scene.Scene$MouseHandler.process(Scene.fx:550)
at javafx.scene.Scene.impl_processMouseEvent(Scene.fx:368)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.fx:496)
at com.sun.javafx.tk.swing.SwingScene$SwingScenePanel.doMouseEvent(SwingScene.java:388)
at com.sun.javafx.tk.swing.SwingScene$SwingScenePanel.mousePressed(SwingScene.java:404)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Hi Rakesh,
Does this combo box has a functionality to select multiple items. for example , if i want to select multiple fields for download like java and javafx so is it possible to select multiple items and show it in a display box ? if yes then how to achieve it ?
Thanks

@Nihar No. ComboBox is usually used for single selection. Will have to use List for multiple selection. Even if we try to enhance the ComboBox, how will user select multiple items in the drop-down List? It will be disposed on selection of one item.

Hi Rakesh,
Great job !!, this control is really missing in the JavaFX 1.2 release.
I have a question about the display of the choice list (listView), why it is directly inserted to the scene content rather than to the parent layout of the combox box control?
Is it possible to change that?
Thanks

Hi Rakesh,
I downloaded Combobox sources into my local system.
And i cleaned and build the sources using Netbeans 6.7.1 IDE. Then i installed the jar file in Windows Mobile 6.0(ASUS Mobile). But the application is not running in the device. If i install the jar file without clean and build, the application is running smoothly. Can you please help in solving this problem.