2014-06-14 62 views
0

在我的应用程序,我hava组合框持有从数据库中的一些值(一些实时更新列表)。该组合框列表每1分钟更新一次。 列表不能有空值。当我设置此列表的ComboBox ..如何在JavaFx的组合框列表中保留“空”选择?

ComboBox box = new ComboBox(items); 

..还有一个额外的“空”行,因为选择的非数值这是完全正常的。 当我选择一些值后,我的“空”值从列表中消失。 我的主要问题是如何将此值保留在列表中?

为什么这是一个问题..

  1. 之情况的值在数据库中选择,第一应用程序启动
    1. 列表被加载(选择室内用空值)。
    2. 选择了值。
    3. 在第一次后台刷新期间,空值消失,并且组合框值被选择为n + 1,下一个值被选中。
  2. 如果我想选择空值,我必须从选择模型/通过一些额外的控制clearSelection。

回答

0

它真的很容易,所有您需要做的是一个" "添加为他们的项目之一,然后选择它:

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; 
    } 
} 
+1

这不是真的有帮助。在字符串作为项目的情况下这是可以的。这不是NULL。是的,你可以指出它,但这不是空的。顺便说一句,如果你gona使用一些其他类作为项目ex。人你会得到异常(从字符串投到人的异常) – kingkong

+0

我看不出这是如何没有帮助,看看这个例子与Person对象。另外:**你将不得不处理它,因为JavaFX并不完全支持你想要的方式。 – Mansueli

+0

尝试在使用person作为item类时将侦听器添加到valueProprty,以及第二种情况..如果您的字符串(对象)与您的值“”在您的情况下可以,但是您不能始终为100%确保你不会有这样的对象 我不是想找到解决方法... – kingkong