ComboBox是JavaFX库的一部分。 JavaFX ComboBox是简单ComboBox的实现,它显示了一个项目列表,用户最多可以从中选择一个项目,它继承了ComboBoxBase类。
ComboBox的构造函数:
ComboBox():创建一个默认的空组合框
ComboBox(ObservableList i):使用给定的项目创建一个组合框
常用方法:
方法
说明
getEditor()
获取属性编辑器的值
getItems()
返回组合框的项目
getVisibleRowCount()
返回属性visibleRowCount的值。
setItems(ObservableList v)设置组合框的项目
setVisibleRowCount(int v)
设置属性VisibleRowCount的值
以下示例程序旨在说明JavaFX的ComboBox类:
程序创建一个组合框并向其中添加项目:此程序创建一个名为combo_box的组合框,并使用ChoiceBox(FXCollections.observableArrayList(week_days))向其中添加字符串列表。我们将组合框和一个标签(描述)添加到磁贴窗格(getChildren().add()函数)。我们将创建一个舞台(容器),并将图块添加到场景中,然后将场景添加到场景中。我们将使用show()函数显示舞台。
// Java Program to create a combo Box and add items to it
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import javafx.stage.Stage;
import javafx.scene.text.Text.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
public class combo_box_1 extends Application {
// Launch the application
public void start(Stage stage)
{
// Set title for the stage
stage.setTitle("creating combo box ");
// Create a tile pane
TilePane r = new TilePane();
// Create a label
Label description_label =
new Label("This is a combo box example ");
// Weekdays
String week_days[] =
{ "Monday", "Tuesday", "Wednesday",
"Thrusday", "Friday" };
// Create a combo box
ComboBox combo_box =
new ComboBox(FXCollections
.observableArrayList(week_days));
// Create a tile pane
TilePane tile_pane = new TilePane(combo_box);
// Create a scene
Scene scene = new Scene(tile_pane, 200, 200);
// Set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// Launch the application
launch(args);
}
}
输出:
程序创建一个组合框并向其中添加一个事件处理程序:此程序创建一个名为combo_box的组合框,并使用(ChoiceBox(FXCollections.observableArrayList(week_days)))向其中添加字符串列表。我们将组合框和一个标签(描述)添加到磁贴窗格(getChildren().add()函数)。我们将创建一个舞台(容器),并将图块添加到场景中,然后将场景添加到场景中。我们将使用show()函数显示舞台。我们将添加一个事件处理程序事件来处理combo_box的事件,该事件会将所选标签的文本更改为所选项目。我们还将选择的标签添加到平铺窗格中。
// Java program to create a combo box and add event handler to it
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import javafx.stage.Stage;
import javafx.scene.text.Text.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
public class combo_box_2 extends Application {
// Launch the application
public void start(Stage stage)
{
// Set title for the stage
stage.setTitle("creating combo box ");
// Create a tile pane
TilePane r = new TilePane();
// Create a label
Label description_label =
new Label("This is a combo box example ");
// Weekdays
String week_days[] =
{ "Monday", "Tuesday", "Wednesday",
"Thrusday", "Friday" };
// Create a combo box
ComboBox combo_box =
new ComboBox(FXCollections
.observableArrayList(week_days));
// Label to display the selected menuitem
Label selected = new Label("default item selected");
// Create action event
EventHandler event =
new EventHandler() {
public void handle(ActionEvent e)
{
selected.setText(combo_box.getValue() + " selected");
}
};
// Set on action
combo_box.setOnAction(event);
// Create a tile pane
TilePane tile_pane = new TilePane(combo_box, selected);
// Create a scene
Scene scene = new Scene(tile_pane, 200, 200);
// Set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// Launch the application
launch(args);
}
}
输出:
注意:以上程序可能无法在在线IDE中运行,请使用离线转换器。