它真的很容易,所有您需要做的是一个" "
添加为他们的项目之一,然后选择它:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ComboBoxSample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override public void start(Stage stage) {
stage.setTitle("ComboBoxSample");
Scene scene = new Scene(new Group(), 450, 250);
final ComboBox<String> fooComboBox = new ComboBox<String>();
fooComboBox.getItems().addAll(
" ",
"foo",
"bar",
"FooBar"
);
fooComboBox.setValue(" ");
HBox box = new HBox();
box.getChildren().add(fooComboBox);
Group root = (Group)scene.getRoot();
root.getChildren().add(box);
stage.setScene(scene);
stage.show();
}
}
示例使用了类人:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class ComboBoxSample extends Application {
ObservableList<Person> people =
FXCollections.observableArrayList(
new Person(null,null),
new Person("Paul", "Mc"),
new Person("Bill", "Apple"),
new Person("Foo", "Bar")
);
@Override
public void start(Stage primaryStage) {
final ComboBox comboBox = new ComboBox(people);
comboBox.getSelectionModel().selectFirst(); //select the first element
comboBox.setCellFactory(new Callback<ListView<Person>,ListCell<Person>>(){
@Override
public ListCell<Person> call(ListView<Person> p) {
final ListCell<Person> cell = new ListCell<Person>(){
@Override
protected void updateItem(Person t, boolean bln) {
super.updateItem(t, bln);
if(t != null){
setText(t.toString());
}else{
setText(null);
}
}
};
return cell;
}
});
final Label label = new Label();
Button btn = new Button();
btn.setText("Read comboBox");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(!(comboBox.getValue().toString()==null))
label.setText("selected: " + comboBox.getValue());
else
label.setText("null");
}
});
VBox vBox = new VBox();
vBox.setPadding(new Insets(5, 5, 5, 5));
vBox.setSpacing(5);
vBox.getChildren().addAll(label, comboBox, btn);
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Combo!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
类人:
public class Person {
private String firstName;
private String lastName;
public Person(String first, String last){
this.lastName = last;
this.firstName = first;
}
public String getName() {
return firstName;
}
public void setName(String name) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setFullName(String first, String last){
this.lastName = last;
this.firstName = first;
}
@Override
public String toString() {
if(firstName != null)
return "" + firstName + " " + lastName;
else
return null;
}
}
这不是真的有帮助。在字符串作为项目的情况下这是可以的。这不是NULL。是的,你可以指出它,但这不是空的。顺便说一句,如果你gona使用一些其他类作为项目ex。人你会得到异常(从字符串投到人的异常) – kingkong
我看不出这是如何没有帮助,看看这个例子与Person对象。另外:**你将不得不处理它,因为JavaFX并不完全支持你想要的方式。 – Mansueli
尝试在使用person作为item类时将侦听器添加到valueProprty,以及第二种情况..如果您的字符串(对象)与您的值“”在您的情况下可以,但是您不能始终为100%确保你不会有这样的对象 我不是想找到解决方法... – kingkong