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).
cheily is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
12