Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing a ComboBox items in JavaFX

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

like image 494
spacitron Avatar asked Jan 16 '14 11:01

spacitron


People also ask

What is a combo box in JavaFX?

ComboBox is a part of the JavaFX library. JavaFX ComboBox is an implementation of simple ComboBox which shows a list of items out of which user can select at most one item, it inherits the class ComboBoxBase.

What is the difference between ComboBox and ChoiceBox JavaFX?

The JavaFX ComboBox control enables users to choose an option from a predefined list of choices, or type in another value if none of the predefined choices matches what the user want to select. The JavaFX ChoiceBox control enables users to choose an option from a predefined list of choices only.

How many items can be selected from a ComboBox at a time?

Only one item at a time can be selected in a combo box, so when the user makes a new selection the previously selected item becomes unselected.


1 Answers

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

like image 83
Jurgen Avatar answered Sep 29 '22 13:09

Jurgen