JavaFX组合框显示除选定项以外的其他条目

时间:2019-01-06 01:14:41

标签: javafx combobox wrapper equals

我注意到,如果两个值根据其equals-method相等(但是根据toString方法具有不同的表示形式,则从列表中选择一个后,在ComboBox中显示的值可能与所选的值不同),因此显示方式不同)。

这可以从下面的示例程序中看到

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Group group = new Group();
            Scene scene = new Scene(group,100,40);

            ObservableList<EqualContent> content = FXCollections.observableArrayList();
            content.add(new EqualContent("A"));
            content.add(new EqualContent("B"));

            Label selection = new Label();

            ComboBox<EqualContent> demoBox = new ComboBox<EqualContent>(content);
            demoBox.setOnAction(event -> selection.setText(" selected: "+demoBox.getValue()));

            group.getChildren().add(new HBox(demoBox, selection));

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    class EqualContent {
        private String name;
        EqualContent(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return name;
        }
        @Override
        public boolean equals(Object other) {
            return other != null;
        }
        @Override
        public int hashCode() {
            return 0;
        }
    }
}

选择项目B会导致以下结果:

enter image description here

此外,之后似乎无法实际选择A。

解决此问题的第一个选择是强制性地修改均等方法,但是我不想这样做,因为我的真实类也有一个compareTo并且我希望它们是有意义的。

第二个选择是围绕EqualContent构建一个包装器类,该包装器类也将String表示形式视为等号。我当然可以做到,但对此并不满意。

我错过了一个更简单或更优雅的解决方案吗?

1 个答案:

答案 0 :(得分:1)

从评论看来,我似乎无法使用包装器。我的示例解决方案是以下通用类(可根据需要提供更多功能,但我已经在其他地方找到了类似的东西):

static class RenameWrapper<T> {
        public final T wrappedObject;
        public final Callback<T, String> renamer;
        RenameWrapper(T wrappedObject, Callback<T, String> renamer) {
            this.wrappedObject = wrappedObject;
            this.renamer = renamer;
        }
        @Override
        public String toString() {
            return renamer.call(wrappedObject);
        }
        public static <S> ArrayList<RenameWrapper<S>> wrapList(List<S> objectsToWrap, Callback<S, String> renamer) {
            ArrayList<RenameWrapper<S>> result = new ArrayList<RenameWrapper<S>>();
            objectsToWrap.forEach(o -> result.add(new RenameWrapper<S>(o, renamer)));
            return result;
        }
        /**
         * This and other are considered equal if other is a RenameWrapper that contains the same
         * wrappedObject according to their equals and the renamer produces the same String representation.
         */
        @Override
        public boolean equals(Object other) {
            if(this == other) return true;
            if(!(other instanceof RenameWrapper)) return false;
            RenameWrapper<?> otherWrapper = (RenameWrapper<?>) other;
            return wrappedObject.equals(otherWrapper.wrappedObject) &&
                    this.toString().equals(other.toString());
        }
        @Override
        public int hashCode() {
            return Objects.hash(wrappedObject, this.toString());
        }
    }

那你就可以做

ObservableList<RenameWrapper<EqualContent>> wrappedContent = FXCollections.observableArrayList(
                    RenameWrapper.wrapList(content, eq -> eq.toString()));

并从wrappedContent而不是以前的内容填充ComboBox。

请注意,在此处使用toString()并不是很好的做法...