2014-09-29 28 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; 
    } 

}