JavaFx:comboBox.setValue不带ActionEvent

JavaFx:comboBox.setValue不带ActionEvent,java,javafx,Java,Javafx,我有一个带有页码(1、2、3等)的组合框。当用户选择某个页面时(通过鼠标或键盘(组合框可编辑))。我想显示和合计页面。这就是为什么在combobox动作中我也会这样做: combobox.setValue(currentPage+"/"+totalPages) 最后的代码看起来像 @FXML private void onPageComboBoxAction(ActionEvent event){ .... combobox.setValue(curr

我有一个带有页码(1、2、3等)的组合框。当用户选择某个页面时(通过鼠标或键盘(组合框可编辑))。我想显示和合计页面。这就是为什么在combobox动作中我也会这样做:

combobox.setValue(currentPage+"/"+totalPages)
最后的代码看起来像

   @FXML
    private void onPageComboBoxAction(ActionEvent event){
       ....
       combobox.setValue(currentPage+"/"+totalPages)
    }
但是,此代码会触发一个或多个ActionEvent和代码周期。因此,我认为有两种可能的方法:

  • 处理一些特定事件(但哪些和如何处理?)
  • 找到一种方法来更改combobox中的值,而无需再触发一个事件(如何再次触发?)
  • 有人能帮我解决这个问题吗?

    试着用设置值代替

    combobox.setPromptText(currentPage+"/"+totalPages);
    
    但我不知道一旦用户选择了值,它是否工作。也许您必须
    setValue(null)
    才能再次看到提示文本。但是您必须将最后选择的值保留在某个位置。

    设置值(…)
    更改组合框保存的值(即,它更改数据或基础模型的状态)。在我看来,你真正想要的只是改变数据的显示方式。通过设置按钮单元格,可以更改选定值的显示

    下面是一个示例,其中按钮单元格同时跟踪所选项目和页数:

    import java.util.Random;
    import java.util.stream.IntStream;
    
    import javafx.application.Application;
    import javafx.collections.ListChangeListener.Change;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.ListCell;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class PageNumberCombo extends Application {
    
        private static final Random RNG = new Random();
    
        @Override
        public void start(Stage primaryStage) {
            ComboBox<Integer> combo = new ComboBox<>();
            combo.setButtonCell(new ListCell<Integer>() {
                {
                    itemProperty().addListener((obs, oldValue, newValue) -> update());
                    emptyProperty().addListener((obs, oldValue, newValue) -> update());
                    combo.getItems().addListener((Change<? extends Integer> c) -> update());
                }
    
                private void update() {
                    if (isEmpty() || getItem() == null) {
                        setText(null);
                    } else {
                        setText(String.format("%d / %d", getItem().intValue(), combo.getItems().size()));
                    }
                }
            });
    
            Button reloadButton = new Button("Reload");
            reloadButton.setOnAction(e -> reload(combo));
    
            reload(combo);
    
            HBox root = new HBox(10, combo, reloadButton);
            root.setAlignment(Pos.CENTER);
            root.setPadding(new Insets(24));
            primaryStage.setScene(new Scene(root, 240, 60));
            primaryStage.show();
        }
    
        private void reload(ComboBox<Integer> combo) {
            int numPages = RNG.nextInt(10) + 11 ;
            combo.getItems().clear();
            IntStream.rangeClosed(1, numPages).forEach(combo.getItems()::add);
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    首先从value属性(如果有)中删除所有侦听器并添加操作事件筛选器,例如,通过添加方法:

    public class MyComboBox extends ComboBox<String> {
        public void setValueSilently(String value) {
            EventHandler<ActionEvent> filter = e -> e.consume();
            valueProperty().removeListener(myValueListener); // a custom listener
            addEventFilter(ActionEvent.ACTION, filter);
            setValue(value);
            removeEventFilter(ActionEvent.ACTION, filter);
            valueProperty().addListener(myValueListener);
        }
    }
    
    公共类MyComboBox扩展了ComboBox{
    public void setvaluesilent(字符串值){
    EventHandler filter=e->e.consume();
    valueProperty().RemovelListener(myValueListener);//自定义侦听器
    addEventFilter(ActionEvent.ACTION,filter);
    设置值(值);
    removeEventFilter(ActionEvent.ACTION,filter);
    valueProperty().addListener(myValueListener);
    }
    }
    
    请将代码添加到您的问题中。@Tomek我编辑了这篇文章。您尝试过我的解决方案吗?@Tomek您的解决方案不是我要找的。因为promtext是promtext,value是value。你可以把它们混合在一起。我的意思是你的解决方案可以奏效。但使用它我们可以忘记promttext的优势。你能澄清一下你在这里的意思吗?即,总页数是可变的,还是固定的?您真的只是希望所选值的显示与下拉列表中的显示不同吗?
    组合框是可编辑的,还是只允许用户从下拉列表中选择?Uff!我以为我被你的代码弄疯了:)。是的,这就是我需要的。但是,如果combobox是可编辑的,则代码不起作用。它能解决吗?嗯,是的,你说过它是可编辑的。我错过了。我想你可以用一个
    StringConverter
    ,但我必须弄清楚。。稍后我将查看是否有时间编辑此内容。我尝试了StringConverter,但据我所知,它不仅用于buttonCell,而且用于所有单元格。因此,这是不可能的吗?不,按照我的建议使用
    StringConverter
    。如果您希望下拉列表中的单元格不同,只需使用常用的
    cellFactory
    。请参阅更新。
    public class MyComboBox extends ComboBox<String> {
        public void setValueSilently(String value) {
            EventHandler<ActionEvent> filter = e -> e.consume();
            valueProperty().removeListener(myValueListener); // a custom listener
            addEventFilter(ActionEvent.ACTION, filter);
            setValue(value);
            removeEventFilter(ActionEvent.ACTION, filter);
            valueProperty().addListener(myValueListener);
        }
    }