2014-09-29 29 views
1

我想有一個組合框與下列選項:JavaFX的組合框一個可編輯的項目

(組合框就業:) - 教育 - 汽車 - (...) - 其他< - 編輯

如果用戶選擇「其他」,他可以編輯ComboBox中的項目,但所有其他選項都是不可編輯的。 當用戶選擇「其他」時,這是可能的還是應該顯示額外的TextField?

回答

1

有設置爲可編輯組合框的選項:

combobox.setEditable(true); 

只能使所有條目編輯使用該功能雖然。 更多詳情,請參閱:http://docs.oracle.com/javafx/2/ui_controls/combo-box.htm

據我所知,只能將字符串添加到包含Combobox內容的ObservableList中。因此你不能添加一個節點(在這種情況下是一個Textfield)。

對於ChoiceBox,如果您在其中添加TextField(技術上可行),但實際使用時只顯示.toString。

因此,您可能最好創建一個單獨的字段。

就像一個想法:當用戶點擊「其他」時,您可以快速創建一個彈出窗口,其中輸入任何其他信息。然後,當你關閉窗口或者點擊enter或者其他什麼時,這個值就會被添加到ObservableList中。將使它看起來更好,我猜...

0

用這個例子:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package comboboxeditable; 

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.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 

/** 
* 
* @author reegan 
*/ 
public class ComboBoxEditable extends Application { 

    Node sub; 

    @Override 
    public void start(Stage primaryStage) { 

     ComboBox mainCombo = new ComboBox(listofCombo()); 
     Button save = new Button("Save"); 
     sub = new ComboBox(listofCombo()); 
     HBox root = new HBox(20); 
     root.getChildren().addAll(mainCombo, sub,save); 

     mainCombo.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { 

      @Override 
      public void changed(ObservableValue observable, Object oldValue, Object newValue) { 
       if (newValue == "Others") { 
        sub = new TextField(); 
       } else { 
        sub = new ComboBox(listofCombo()); 
       } 
       root.getChildren().remove(1); 
       root.getChildren().add(1, sub); 
      } 
     }); 
     save.setOnAction(new EventHandler<ActionEvent>() { 

      @Override 
      public void handle(ActionEvent event) { 
       System.out.println(mainCombo.getValue()); 
       if(sub.getClass() == ComboBox.class) { 
        ComboBox sub1 = (ComboBox)sub; 
        System.out.println(sub1.getValue()); 
       } else { 
        TextField field = (TextField)sub; 
        System.out.println(field.getText()); 
       } 
      } 
     }); 


     Scene scene = new Scene(root, 300, 250); 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

    public ObservableList listofCombo() { 
     ObservableList<String> list = FXCollections.observableArrayList(); 
     for (int i = 0; i < 10; i++) { 
      list.add(String.valueOf("Hello" + i)); 
     } 
     list.add("Others"); 
     return list; 
    } 

}