JavaFX ChoiceBox

 

ChoiceBox



§  Another variation on the list control is ChoiceBox. ChoiceBox is somewhat like a ComboBox but without the editing capabilities. It lets the user select an option from a drop-down list. The selection is shown as checked in the list. Only single selection is supported. This makes ChoiceBox a useful alternative to radio buttons when space is an issue.

§  ChoiceBox is a generic class that is declared like this:

§  class ChoiceBox<T>

§  Here, T specifies the type of entries. As with the other list controls, these are often entries of type String.

 

§  ChoiceBox defines two constructors. The first is the default constructor, which creates an empty

§  ChoiceBox. The second lets you specify the list of entries. It is shown here: ChoiceBox(ObservableList<T> list)

§  Here, list specifies a list of the items that will be displayed. It is an object of type ObservableList. As previously explained, ObservableList defines a list of observable objects, and an easy way to create an ObservableList is to use the factory method observableArrayList( ), defined by the FXCollections class.

§  A ChoiceBox is managed much like a ListView. For example, you can monitor the list for changes by registering a change listener on the selection model in much the same way as shown for ListView. Doing so lets you respond each time the user changes a selection in the list. To listen for change events, you must first obtain the selection model. This is done by calling getSelectionModel( ) on the ChoiceBox. It is shown here:

§  final SingleSelectionModel<T> getSelectionModel( )

§  It returns a reference to the model. SingleSelectionModel is a class that defines the model used for single selections. As mentioned, ChoiceBox supports only single selection. (If you need multiple selection, consider using a ListView.) Next, add the change listener to the object returned by selectedItemProperty( ) when called on the selection model.

§  You can set the current value of the control by calling setValue( ). You can obtain the value by calling

§  getValue( ).

§  Often, the default size of the control is appropriate, but you can set the size explicitly by use of

§  setPrefWidth( ), setPrefHeight( ), or setPrefSize( ).

§  The following program demonstrates ChoiceBox by reworking the ListViewDemo example shown earlier so that it uses a choice box instead of a list view:

 

// JavaFX Controls

// ChoiceBox

 

import javafx.application.*;

import javafx.scene.*;

import javafx.stage.*;

import javafx.event.*;

import javafx.scene.layout.*;

import javafx.scene.control.*;

import javafx.scene.input.*;

import javafx.scene.text.*;

import javafx.geometry.*;

import javafx.beans.value.*;

import javafx.collections.*;

 

public class JfxChk extends Application

{

   public static void main(String []args)

   {

        launch(args);    

   }

  

   public void init(){}

   public void stop(){}

  

   public void start(Stage myStage)

   {

        myStage.setTitle("JavaFX - ListView");

  

        FlowPane fp = new FlowPane(Orientation.VERTICAL,15,15);

        fp.setAlignment(Pos.CENTER);

       

        Label L1 = new Label();

       

        // Create list and add it to ViewList

        ObservableList<String> dept = FXCollections.observableArrayList(

"IT","CSE", "CIVIL", "MECH", "Others");

        ChoiceBox<String> choice = new ChoiceBox<String>(dept);

        choice.setValue("IT");

       

       

        SingleSelectionModel<String> choicesel = choice.getSelectionModel();

       

        // Event Handling – ChangeListener - Item

        choicesel.selectedItemProperty().addListener(new ChangeListener<String>()

        {

             public void changed(ObservableValue<? extends String> changed,

String oldval, String newval)

             {

                 L1.setText("Selected department is "+newval);

             }

        });

                

        fp.getChildren().addAll(choice, L1);

       

        Scene myScene = new Scene(fp, 300, 200);

        myStage.setScene(myScene);

        myStage.show();

   }

}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu