-
-
Save AndresFRodriguezR/1b398fcc958550a45ac2190ff8f22dec to your computer and use it in GitHub Desktop.
Hover over a JavaFX Label to reveal a ComboBox to change the label's text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.geometry.*; | |
import javafx.scene.Scene; | |
import javafx.scene.control.*; | |
import javafx.scene.layout.*; | |
import javafx.stage.Stage; | |
public class Hoverboard extends Application { | |
public class TextChooser extends StackPane { | |
private Label label = new Label(); | |
private ComboBox<String> combo = new ComboBox<>(); | |
public TextChooser(String... options) { | |
StackPane.setAlignment(label, Pos.CENTER_LEFT); | |
StackPane.setAlignment(combo, Pos.CENTER_LEFT); | |
label.textProperty().bind( | |
combo.getSelectionModel().selectedItemProperty() | |
); | |
label.visibleProperty().bind( | |
combo.visibleProperty().not() | |
); | |
label.setPadding(new Insets(0, 0, 0, 9)); | |
combo.getItems().setAll(options); | |
combo.getSelectionModel().select(0); | |
combo.setVisible(false); | |
label.setOnMouseEntered(event -> combo.setVisible(true)); | |
combo.showingProperty().addListener(observable -> { | |
if (!combo.isShowing()) { | |
combo.setVisible(false); | |
} | |
}); | |
combo.setOnMouseExited(event -> { | |
if (!combo.isShowing()) { | |
combo.setVisible(false); | |
} | |
}); | |
getChildren().setAll(label, combo); | |
} | |
} | |
@Override | |
public void start(Stage stage) throws Exception { | |
TextChooser textChooser = new TextChooser( | |
"xyzzy", "frobozz", "foobar" | |
); | |
VBox layout = new VBox(textChooser); | |
layout.setPadding(new Insets(10)); | |
stage.setScene(new Scene(layout)); | |
stage.show(); | |
} | |
public static void main(String[] args) { | |
launch(Hoverboard.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment