【发布时间】:2014-11-13 12:59:42
【问题描述】:
我自定义了一个组合框,以便在组合框中添加和删除项目。 现在我将绑定我的自定义组合框,但它不起作用,我不明白会发生什么。
这是我的示例:
public class CbbxEditSuppr extends ComboBox<BoxItem> {
private TextField editor = null;
private ObservableList<BoxItem> items = null;
/**
* Constructeur.
*
* @param addedItems ObservableList<String>
* @param prefWidth double largeur préférée
*/
public CbbxEditSuppr(ObservableList<String> addedItems, final double prefWidth) {
// initialisation des attributs
editor = this.getEditor();
items = this.getItems();
this.setPrefWidth(prefWidth);
// initialisation du contenu de la cellule
this.setCellFactory(new Callback<ListView<BoxItem>, ListCell<BoxItem>>() {
@Override
public ListCell<BoxItem> call(ListView<BoxItem> p) {
final ListCell<BoxItem> cell = new ListCell<BoxItem>() {
@Override
protected void updateItem(BoxItem t, boolean bln) {
super.updateItem(t, bln);
if (t != null) {
setGraphic(t.getBtn());
setText(t.getItem());
} else {
setGraphic(null);
}
}
};
return cell;
}
});
// déininition du converter
this.setConverter(new StringConverter<BoxItem>() {
@Override
public String toString(BoxItem cbbx) {
if (cbbx == null) {
return null;
} else {
return cbbx.getItem();
}
}
@Override
public BoxItem fromString(String id) {
if (id != null) {
final BoxItem box = new BoxItem(id, items);
return box;
} else {
return null;
}
}
});
// permet de prendre en compte la touche ENTER, et ajouter des valeurs a la liste
this.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
public void handle(final KeyEvent event) {
if (event != null && event.getCode().equals(KeyCode.ENTER)) {
if (editor.getText().trim().length() > 0) {
addItem(editor.getText());
editor.clear();
}
} else if (event != null && event.getCode().equals(KeyCode.DOWN)) {
showPopUpMenu();
}
}
});
// propriétés editable et selection du premier element
this.setEditable(true);
/* ajout des valeurs a la liste d'items */
if (addedItems != null && addedItems.size() > 0) {
for (String stg : addedItems) {
if (stg != null) {
final BoxItem hbox = new BoxItem(stg, items);
items.add(hbox);
}
}
}
this.getSelectionModel().selectFirst();
}
private void showPopUpMenu(){
if (!this.isShowing()) {
this.show();
}
}
/**
* Ajoute un item à la liste
*
* @param stg String nom de l'item
*/
public void addItem(String stg) {
if (stg != null) {
items.add(new BoxItem(stg, items));
}
}
/**
* Retourne la description du contenu de la liste
*/
public String toString() {
final StringBuilder stgBuilder = new StringBuilder("[ ");
if (items != null && items.size() > 0) {
final BoxItem lastItem = items.get(items.size() - 1);
for (BoxItem item : items) {
if (item != null) {
stgBuilder.append(item.getItem());
if (!item.equals(lastItem)) {
stgBuilder.append(", ");
}
}
}
}
stgBuilder.append(" ]");
return stgBuilder.toString();
}
}
和
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);
CbbxEditSuppr cbboxLeft = new CbbxEditSuppr(FXCollections.observableArrayList(new ArrayList()), 200);
CbbxEditSuppr cbboxRight = new CbbxEditSuppr(FXCollections.observableArrayList(new ArrayList()), 200);
cbboxLeft.itemsProperty().bindBidirectional(cbboxRight.itemsProperty());
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setHgap(10);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("To: "), 0, 0);
grid.add(cbboxLeft, 1, 0);
grid.add(cbboxRight, 2, 0);
Group root = (Group) scene.getRoot();
root.getChildren().add(grid);
stage.setScene(scene);
stage.show();
}
}
当我在右侧组合框中添加一个值时,它正在工作,但在左侧组合框中却没有。 请问您有什么建议吗?
【问题讨论】:
-
什么是“工作”,什么是“不工作”?你期望什么以及它的表现如何?你有什么错误吗?如果有,你有堆栈跟踪吗?
-
我的示例显示了 2 个组合框:comboboxLeft 和 comboboxright。此组合框已绑定。我没有堆栈跟踪,我的问题来自组合框的行为。当我在右侧组合框中写入并按 ENTER 时,文本将作为项目添加到组合框中。此项目添加在两个组合框中。现在,当我在左侧组合框中执行相同操作时,该项目不会添加到组合框中。这是我的问题。绑定仅在一个方向上起作用:(
标签: binding combobox javafx-2 items