Managing a ComboBox items in JavaFX

13,902

You shouldn't be putting the Text nodes into the ComboBox ... please see this question that will help you: setButtonCell for ComboBox

Share:
13,902
spacitron
Author by

spacitron

Updated on June 21, 2022

Comments

  • spacitron
    spacitron almost 2 years

    I'm populating a ComboBox using Text objects. Using Text objects rather than Strings allows me to add an id value that I can use in my program and later exploit when I decide to internationalize the UI. Anyway, here is what I'm doing: Main class:

    public class MainApp extends Application {
    
    private Stage primaryStage;
    
    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        try {
            AnchorPane paneMain = (AnchorPane) FXMLLoader.load(getClass().getResource("Test.fxml"));
            Scene scene = new Scene(paneMain);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        launch(args);
    }
    

    }

    Controller:

    public class Test implements Initializable{
    
    @FXML
    private AnchorPane anchorPane;
    @FXML
    private ComboBox<Text> comboTime; 
    private Text days;
    private Text hours;
    private Text minutes;
    private int timeMultiplier; 
    
    public Test(){
        days = new Text("Days");
        days.setId("86400000");
        hours = new Text("Hours");
        hours.setId("3600000");
        minutes = new Text("Minutes");
        minutes.setId("3600000");
        timeMultiplier = 0;
    }
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        comboTime.getItems().removeAll(comboTime.getItems());
        comboTime.getItems().addAll(days, hours, minutes);
        comboTime.getSelectionModel().select(hours);
    
    }
    
    @FXML
    private void setTimeMultiplier(){
        Text text = comboTime.getSelectionModel().getSelectedItem();
        timeMultiplier = Integer.valueOf(text.getId());
    }
    
    }
    

    Test.fxml:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.collections.*?>
    <?import javafx.geometry.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.paint.*?>
    <?import javafx.scene.text.*?>
    
    <AnchorPane id="AnchorPane" fx:id="anchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.99990000000253" prefWidth="94.99990000000253" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.spacitron.backupp.ui.controllers.Test">
      <children>
        <HBox id="HBox" alignment="CENTER" layoutX="41.0" layoutY="224.0" prefWidth="216.0" spacing="5.0" />
        <ComboBox id="comboInterval" fx:id="comboTime" editable="false" layoutX="14.0" layoutY="22.0" onAction="#setTimeMultiplier">
          <items>
            <FXCollections fx:factory="observableArrayList">
              <String fx:value="Item 1" />
              <String fx:value="Item 2" />
              <String fx:value="Item 3" />
            </FXCollections>
          </items>
        </ComboBox>
      </children>
    </AnchorPane>
    

    Now, this works just fine. The problem however is that when I select an item the text on that item goes blank like so:

    enter image description here

    And if I select another one, that disappears too:

    enter image description here

    I can still select the items, but they're just not there to be seen. Is this a bug and if so is there a way around it?

    EDITED to provide MCVE