Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Last active May 19, 2022 09:16
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jewelsea/1422104 to your computer and use it in GitHub Desktop.
Save jewelsea/1422104 to your computer and use it in GitHub Desktop.
Example JavaFX ChoiceBox control backed by Database IDs
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ChoiceBoxBackedByDatabaseIds extends Application {
public static void main(String[] args) throws Exception { launch(args); }
public void start(final Stage stage) throws Exception {
// create a backing database;
final Database db = new Database();
// provide some instructions.
final Label instructions = new Label("Choose an opponent.");
instructions.setMouseTransparent(true);
// provide some choices based on the animals in the database;
ObservableList<Choice> choices = FXCollections.observableArrayList();
choices.add(new Choice(null, "No selection"));
for (Database.Animal animal : db.findAllAnimals()) {
choices.add(new Choice(animal.id, animal.name));
}
final ChoiceBox<Choice> chooser = new ChoiceBox<>(choices);
chooser.getSelectionModel().select(0);
// act on a choice.
chooser.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Choice>() {
@Override public void changed(ObservableValue<? extends Choice> observableValue, Choice oldChoice, Choice newChoice) {
if (newChoice.id == null) {
instructions.setText("You have chosen cowardice.");
} else {
// lookup info by id in the database.
Database.Animal opponent = db.findAnimal(newChoice.id);
StringBuilder weapons = new StringBuilder();
for (int weaponId : opponent.weaponIds) {
weapons.append("\n ").append(db.findWeapon(weaponId).name);
}
// format and display the lookup info.
instructions.setText(
"You chosen to face the " + opponent.name + ".\n\n" +
"Beware of your opponents deadly attacks:" + weapons + "\n\n" +
"Good Luck!"
);
}
}
});
// show the scene.
final VBox layout = new VBox(20);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10; -fx-font-size: 20;");
layout.getChildren().addAll(instructions, chooser);
Scene scene = new Scene(layout, 550, 350);
stage.setScene(scene);
stage.show();
}
/** Helper class for mapping a choice displayable in a ChoiceBox to a backing id. */
class Choice {
Integer id; String displayString;
Choice(Integer id) { this(id, null); }
Choice(String displayString) { this(null, displayString); }
Choice(Integer id, String displayString) { this.id = id; this.displayString = displayString; }
@Override public String toString() { return displayString; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Choice choice = (Choice) o;
return displayString != null && displayString.equals(choice.displayString) || id != null && id.equals(choice.id);
}
@Override public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (displayString != null ? displayString.hashCode() : 0);
return result;
}
}
/** Quick and dirty in memory database */
class Database {
private Weapon[] weapons = {
new Weapon(1, "Flaming Breath"), new Weapon(2, "Feathers of Doom"), new Weapon(3, "Sleeping Sickness"), new Weapon(4, "Drop from above"), new Weapon(5, "Laughter")
};
private Animal[] animals = {
new Animal(1, "Burning Emu", 1, 2 ), new Animal(2, "Killer Koala", 3, 4), new Animal(3, "Outrageous Orangutan", 5)
};
Animal findAnimal(int id) { for (Animal animal : animals) if (animal.id == id) return animal; return null; }
Animal[] findAllAnimals() { return animals; }
Weapon findWeapon(int id) { for (Weapon weapon : weapons) if (weapon.id == id) return weapon; return null; }
class Animal {
int id; String name; int[] weaponIds;
Animal(int id, String name, int... weaponIds) { this.id = id; this.name = name; this.weaponIds = weaponIds; }
@Override public String toString() { return name; }
}
class Weapon {
int id; String name;
Weapon(int id, String name) { this.id = id; this.name = name; }
@Override public String toString() { return name; }
}
}
}
@jewelsea
Copy link
Author

jewelsea commented Dec 2, 2011

@Abaserve
Copy link

Abaserve commented Jan 1, 2012

Great, Have you figured out to do the reverse, prompt choicebox from database: say i have Database A abc, B bcd and C cde with
choicebox {abc, bcd, cde}. If I want to prompt bcd
To use choice.getSelectionModel().select(1) where 1 is the index, I need the index for the id (A,B,C) , any idea how to get it.
Your Example:
chooser.getSelectionModel().select("Killer Koala");
or the ID
chooser.getSelectionModel().select(2); (that is ID 2 not index 2

@jewelsea
Copy link
Author

jewelsea commented Jan 5, 2012

I updated the gist to include an equivalence implementation for the Choice class so that you can perform selections using:
chooser.getSelectionModel().select(new Choice(2)) OR chooser.getSelectionModel().select(new Choice("Killer Koala"))

I added a comment to the linked forum thread to explain this in more detail.

@Abaserve
Copy link

Abaserve commented Jan 5, 2012

Thanks so much, I struggled with this one!

@Region40
Copy link

Region40 commented Aug 1, 2017

Saved me a lot of work with such a simple concept. Thanks!

@AvocadoFlour
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment