如何从javaFX中的ComobBox中获取文本?

时间:2018-09-05 10:18:50

标签: java javafx

如果即时通讯要从文本字段中获取文本,则将是这样:

String username = txt_username.getText();

但是如何从ComboBox获取文本?

我尝试过:

int TableNo = (int)comboBoxOrder.getItems();

1 个答案:

答案 0 :(得分:0)

comboBox.getValue()将返回所选对象,因此您可能想要.toString()

public class MainNoFXML extends Application {

    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage primaryStage) {
        ComboBox comboBox = new ComboBox(FXCollections.observableArrayList(new String[]{"Monday", "Tuesday", "Wednesday", "Thrusday", "Friday"}));
        comboBox.setOnAction(event -> {
            System.out.println("Selected:"+comboBox.getValue().toString());
            System.out.println("All:"+comboBox.getItems().toString());
        });

        Scene scene = new Scene(comboBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}