JavaFX Button set to disable on click causes StackOverflowError

I have a ComboBox in which I store a list of labels and I’ve added an extra row in which i allow the user to write their own label and add it to the list.
Described combobox with the edition row on the bottom, the reload button is not relevant here.

This whole row is added programatically with the use of the ComboBoxListViewSkin.
Within this row is the Button in question – the “+Add” button, working in tandem with the TextField right next to it. It becomes enabled whenever the textfield’s content is not blank and disables when it is, including right after submitting the newly created label.

Minimum Reproducible Example application class:

package org.example;

import javafx.application.Application;
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.control.skin.ComboBoxListViewSkin;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        ComboBox<String> comboBox = new ComboBox<>();
        ComboBoxListViewSkin<String> comboBoxListViewSkin = new ComboBoxListViewSkin<String>(comboBox) {
            private Button buttonAdd = new Button("+Add");

            private TextField textField = new TextField();
            private VBox pane = new VBox();
            {
                buttonAdd.setDisable(true);
                buttonAdd.setOnAction(event -> {
                    String text = textField.getText();
                    if (text.isBlank()) return;
                    comboBox.getItems().add(text);
                    textField.clear();
                    buttonAdd.setDisable(true);
                });
                textField.setOnKeyTyped(event -> {
                    if (textField.getText().isBlank())
                        buttonAdd.setDisable(true);
                    else buttonAdd.setDisable(false);
                });

                textField.setOnAction(event -> buttonAdd.fire());
            }

            @Override
            public Node getPopupContent() {
                Node defaultContent = super.getPopupContent();
                HBox hBox = new HBox();
                hBox.getChildren().addAll(buttonAdd, textField);
                HBox.setHgrow(textField, Priority.ALWAYS);
                defaultContent.setManaged(true);
                pane.getChildren().setAll(defaultContent, hBox);
                return pane;
            }
        };
        comboBox.setSkin(comboBoxListViewSkin);
        comboBox.setPrefWidth(400);
        comboBox.setPrefHeight(30);
        VBox vBox = new VBox(comboBox);

        Scene scene = new Scene(vBox, 400, 100);
        stage.setScene(scene);
        stage.show();
    }

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

And that’s where the problem lies, whenever the label is submitted with this button it causes a StackOverflowError. This MRE made me notice that this only happens on the second and later additions, so perhaps it’s some sort of ‘editing during iteration’ kind of issue?
I will note that the change is also correctly propagated into my app’s data structure, the UI is updated accordingly and there do not seem to be any other issues with the program except for this error loop in the logs:

Exception in thread "JavaFX Application Thread" java.lang.StackOverflowError
    at com.sun.glass.ui.Accessible$GetAttribute.run(Accessible.java:144)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at com.sun.glass.ui.Accessible.lambda$getAttribute$0(Accessible.java:172)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at com.sun.glass.ui.Accessible.getAttribute(Accessible.java:169)
    at com.sun.glass.ui.win.WinAccessible.sendNotification(WinAccessible.java:273)
    at com.sun.glass.ui.win.WinAccessible.sendNotification(WinAccessible.java:281)
// ...

The trace suggests it’s a rendering/ui issue?

The troublemaker seems to be this buttonAdd.setDisable(true) at the end of the setOnAction lambda, as removing it has caused the error to not appear anymore.

I am not sure why this causes the issue.
I thought that perhaps calling the textField.clear() might trigger its own onAction and refire the button but that does not seem to be the case and neither does it seem to be stuck rerunning the button’s onAction.


Edit:
This only seems to happen when using the button directly with a mouseclick and not when pressing Enter within the textfield (propagated buttonAdd.fire()).

I’m using JavaFX 19.0.2.1 on Windows 10 (22H2).

New contributor

cheily is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

12

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật