Issue
I want to be able to always show the popup part of a combobox regardless of the fact it has been clicked or not, or even without the combo being focused. I tried to use the show()
method of the combo, but in my case the popup part never shows.
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class ComBoBoxTest {
public static final void main(String[] args) {
ComBoBoxTest test = new ComBoBoxTest();
test.setup();
}
private void setup() {
new JFXPanel();
Platform.runLater(new Runnable() {
@Override
public void run() {
createUI();
}
});
}
private void createUI() {
Stage stage = new Stage();
Pane pane = new Pane();
ComboBox<String> combo = new ComboBox();
ObservableList<String> values = combo.getItems();
values.add("ONE");
values.add("TWO");
values.add("THREE");
combo.setVisibleRowCount(3);
combo.show();
pane.getChildren().add(combo);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show(); // the important part
}
}
In that case, I thought that my show()
method would force to open the popup, but the result is that the popup is never shown
Solution
Per the excellent comment of kleopatra, the solution is to call show()
on the Combo after the Stage is shown. This example works:
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class ComBoBoxTest {
public static final void main(String[] args) {
ComBoBoxTest test = new ComBoBoxTest();
test.setup();
}
private void setup() {
new JFXPanel();
Platform.runLater(new Runnable() {
@Override
public void run() {
createUI();
}
});
}
private void createUI() {
Stage stage = new Stage();
Pane pane = new Pane();
ComboBox<String> combo = new ComboBox();
ObservableList<String> values = combo.getItems();
values.add("ONE");
values.add("TWO");
values.add("THREE");
combo.setVisibleRowCount(3);
pane.getChildren().add(combo);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
combo.show(); // call show() on the Combo after the stage is shown
}
}
Answered By - Hervé Girod
Answer Checked By - Willingham (JavaFixing Volunteer)