Skip to content

Instantly share code, notes, and snippets.

@TheItachiUchiha
Created June 22, 2017 12:13
Show Gist options
  • Save TheItachiUchiha/20e1299a9dd95a0ee7d180343530a67c to your computer and use it in GitHub Desktop.
Save TheItachiUchiha/20e1299a9dd95a0ee7d180343530a67c to your computer and use it in GitHub Desktop.
Bind Button Disable Property to ListView Selection
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ShowCaseBooleanProperty extends Application {
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
ListView<String> lv = new ListView<>();
lv.getItems().addAll("Example");
Button clearSelection = new Button("Clear Selection");
clearSelection.setOnAction(e -> lv.getSelectionModel().select(null));
Button btnBindButtonToProp = new Button("Button -> Prop");
BorderPane bp = new BorderPane();
bp.setCenter(lv);
HBox hb = new HBox();
hb.getChildren().addAll(btnBindButtonToProp, clearSelection);
bp.setBottom(hb);
btnBindButtonToProp.disableProperty().bind(lv.getSelectionModel().selectedItemProperty().isNotNull());
primaryStage.setScene(new Scene(bp, 400, 200));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
@ThatGuyPancake
Copy link

Thanks a lot, line 27 really helped me in a uni project! 👍

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