Combobox class in java

Combobox class in java

By default, when the popup list is showing, the maximum number of rows visible is 10, but this can be changed by modifying the visibleRowCount property. If the number of items in the ComboBox is less than the value of visibleRowCount , then the items size will be used instead so that the popup list is not exceedingly long.

As with ListView, it is possible to modify the selection model that is used, although this is likely to be rarely changed. This is because the ComboBox enforces the need for a SingleSelectionModel instance, and it is not likely that there is much need for alternate implementations. Nonetheless, the option is there should use cases be found for switching the selection model.

As the ComboBox internally renders content with a ListView, API exists in the ComboBox class to allow for a custom cell factory to be set. For more information on cell factories, refer to the Cell and ListCell classes. It is important to note that if a cell factory is set on a ComboBox, cells will only be used in the ListView that shows when the ComboBox is clicked. If you also want to customize the rendering of the ‘button’ area of the ComboBox, you can set a custom ListCell instance in the button cell property. One way of doing this is with the following code (note the use of setButtonCell :

 Callback, ListCell> cellFactory = . ; ComboBox comboBox = new ComboBox(); comboBox.setItems(items); comboBox.setButtonCell(cellFactory.call(null)); comboBox.setCellFactory(cellFactory);

Because a ComboBox can be editable , and the default means of allowing user input is via a TextField , a string converter property is provided to allow for developers to specify how to translate a users string into an object of type T, such that the value property may contain it. By default the converter simply returns the String input as the user typed it, which therefore assumes that the type of the editable ComboBox is String. If a different type is specified and the ComboBox is to be editable, it is necessary to specify a custom StringConverter .

Читайте также:  Java programming with applet

A warning about inserting Nodes into the ComboBox items list

ComboBox allows for the items list to contain elements of any type, including Node instances. Putting nodes into the items list is strongly not recommended. This is because the default cell factory simply inserts Node items directly into the cell, including in the ComboBox ‘button’ area too. Because the scenegraph only allows for Nodes to be in one place at a time, this means that when an item is selected it becomes removed from the ComboBox list, and becomes visible in the button area. When selection changes the previously selected item returns to the list and the new selection is removed.

The recommended approach, rather than inserting Node instances into the items list, is to put the relevant information into the ComboBox, and then provide a custom cell factory . For example, rather than use the following code:

 ComboBox cmb = new ComboBox(); cmb.getItems().addAll( new Rectangle(10, 10, Color.RED), new Rectangle(10, 10, Color.GREEN), new Rectangle(10, 10, Color.BLUE));

You should do the following:

 ComboBox cmb = new ComboBox(); cmb.getItems().addAll( Color.RED, Color.GREEN, Color.BLUE); cmb.setCellFactory(new Callback() < @Override public ListCellcall(ListView p) < return new ListCell() < private final Rectangle rectangle; < setContentDisplay(ContentDisplay.GRAPHIC_ONLY); rectangle = new Rectangle(10, 10); >@Override protected void updateItem(Color item, boolean empty) < super.updateItem(item, empty); if (item == null || empty) < setGraphic(null); >else < rectangle.setFill(item); setGraphic(rectangle); >> >; > >);

Admittedly the above approach is far more verbose, but it offers the required functionality without encountering the scenegraph constraints.

Property Summary

Providing a custom cell factory allows for complete customization of the rendering of items in the ComboBox.

Converts the user-typed input (when the ComboBox is editable ) to an object of type T, such that the input may be retrieved via the value property.

Properties inherited from class javafx.scene.control.ComboBoxBase

Properties inherited from class javafx.scene.control.Control

Properties inherited from class javafx.scene.layout.Region

Properties inherited from class javafx.scene.Parent

Properties inherited from class javafx.scene.Node

Field Summary

Fields inherited from class javafx.scene.control.ComboBoxBase

Fields inherited from class javafx.scene.layout.Region

Fields inherited from class javafx.scene.Node

Constructor Summary

Method Summary

Providing a custom cell factory allows for complete customization of the rendering of items in the ComboBox.

Converts the user-typed input (when the ComboBox is editable ) to an object of type T, such that the input may be retrieved via the value property.

Methods inherited from class javafx.scene.control.ComboBoxBase

Methods inherited from class javafx.scene.control.Control

Methods inherited from class javafx.scene.layout.Region

Methods inherited from class javafx.scene.Parent

Methods inherited from class javafx.scene.Node

Methods inherited from class java.lang.Object

Property Detail

items

public ObjectPropertyObservableListT>> itemsProperty

converter

public ObjectPropertyStringConverterT>> converterProperty

Converts the user-typed input (when the ComboBox is editable ) to an object of type T, such that the input may be retrieved via the value property.

cellFactory

public ObjectPropertyCallbackListViewT>,ListCellT>>> cellFactoryProperty

Providing a custom cell factory allows for complete customization of the rendering of items in the ComboBox. Refer to the Cell javadoc for more information on cell factories.

buttonCell

public ObjectPropertyListCellT>> buttonCellProperty

The button cell is used to render what is shown in the ComboBox ‘button’ area. If a cell is set here, it does not change the rendering of the ComboBox popup list — that rendering is controlled via the cell factory API.

selectionModel

public final ObjectPropertySingleSelectionModelT>> selectionModelProperty

visibleRowCount

The maximum number of rows to be visible in the ComboBox popup when it is showing. By default this value is 10, but this can be changed to increase or decrease the height of the popup.

editor

placeholder

This Node is shown to the user when the ComboBox has no content to show. The placeholder node is shown in the ComboBox popup area when the items list is null or empty.

Constructor Detail

ComboBox

ComboBox

Источник

Оцените статью