JavaFX ComboBox

 

ComboBox


§  JavaFX provides a variation of a list control called the combo box, which is implemented by the ComboBox class. A combo box displays one selection, but it will also display a drop-down list that allows the user to select a different item. Additionally, you can allow the user to edit a selection. ComboBox inherits ComboBoxBase, which provides much of its functionality. Unlike ListView, which can allow multiple selections, ComboBox is designed for single selection.

§  ComboBox is a generic class that is declared like this: class ComboBox<T>

 

§  Here, T specifies the type of entries. Often, these are entries of type String, but other types are also allowed.

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

§  ComboBox. The second lets you specify the entries in the list. It is shown here: ComboBox(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. It inherits java.util.List. As also previously explained, an easy way to create an ObservableList is to use the factory method observableArrayList( ), which is a static method defined by the FXCollections class.

§  A ComboBox generates an action event when its selection changes. It will also generate a change event.

§  Alternatively, it is also possible to ignore events and simply obtain the current selection when needed.

§  You can obtain the current selection by calling getValue( ), shown here: final T getValue( )

§  If the value of a combo box has not yet been set (by the user or under program control), getValue( ) will return null. To set the value of a ComboBox under program control, call setValue( ):

§  final void setValue(T newVal)

§  Here, newVal becomes the new value.

§  The following program demonstrates a combo box by reworking the previous ListView example. It handles action events generated by the combo box.


 

// JavaFX Controls

// ComboBox

 

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 - ComboBox");

  

        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");

        ComboBox<String> cbox = new ComboBox<String>(dept);

        cbox.setValue("IT");

                

        // Event Handling

        cbox.setOnAction(new EventHandler<ActionEvent>()

        {

             public void handle(ActionEvent ae)

             {

                 L1.setText("Selected department is "+cbox.getValue());

             }

        });

       

        fp.getChildren().addAll(cbox, 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