JavaFX Java GUI Tutorial - 13 - Listening for Selection Changes

Поділитися
Вставка
  • Опубліковано 10 жов 2024
  • Source Code: github.com/the...
    Core Deployment Guide (AWS): docs.google.co...

КОМЕНТАРІ • 51

  • @redhatzeno
    @redhatzeno 8 років тому +26

    Dude, i just wanted to let you know I'm a huge fan of your teaching style. So far I've only seen your JavaFX tutorials, but can't wait to see other stuff you have!
    God bless!

  • @dinkarinjosh
    @dinkarinjosh 4 роки тому +6

    Update: setOnAction was added from 8.

  • @vishaalshiva2009
    @vishaalshiva2009 9 років тому +12

    You actually make javafx really easy!!!

  • @jesusemanuel4976
    @jesusemanuel4976 5 років тому +3

    you are awesome my friend!!! God Bless You!!!

  • @mellonviskaino9537
    @mellonviskaino9537 2 роки тому

    Thank you sooooo much I was searching for hours to get how to implement the listener

  • @crazyotaku5754
    @crazyotaku5754 3 роки тому

    You are the most awesome teacher i have ever seen !!!!! God bless you

  • @williamfoster4883
    @williamfoster4883 3 роки тому

    You safed my day! Thanks a lot!

  • @rohan_ray
    @rohan_ray 6 років тому +1

    can also be done as....
    public class Main extends Application{
    public static void main(String[] args){
    launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
    Stage sg = new Stage();
    sg.setTitle("MY WINDOW");
    VBox vx = new VBox(20);
    vx.setPadding(new Insets(10 ,50 ,10,10));
    ChoiceBox cb = new ChoiceBox();
    cb.getItems().addAll("Apple","Banana","Coconut","Oranges","Grapes");
    cb.getSelectionModel().select(0);
    cb.setOnAction(e-> System.out.println(cb.getValue()));
    vx.getChildren().addAll(new Label("Select Your Choice" ),cb ,new Button("click") );
    sg.setScene(new Scene(vx ,300 ,250));
    sg.show();
    }
    }

  • @EscPointDev
    @EscPointDev 6 років тому

    Also if anyone is interested you can use selectedIndexProperty() in the same place as selectedItemProperty() making so you get the index old and new rather than the strings which can be more useful.

  • @haliluluoglu1188
    @haliluluoglu1188 3 роки тому

    Thanks a lot, you're life savior :)

  • @SenseiEli
    @SenseiEli 4 роки тому

    Thank you again!

  • @osrs5069
    @osrs5069 8 років тому

    Awesome video.
    making your custom listener is awesome instead let Sun Microsoft develop everything for us.

  • @rimantasri4578
    @rimantasri4578 4 роки тому +1

    The whole video is about one line of code xd.
    P.S. I like your videos!

  • @amicusimperatori
    @amicusimperatori 6 років тому

    Very useful tutorial. Thanks for sharing knowledge.

  • @lechi_2002
    @lechi_2002 8 років тому +15

    choiceBoxs.setOnAction(e -> System.out.println(colors.getValue()));
    It's much easier this way.

    • @MrRight1000
      @MrRight1000 8 років тому +4

      This is indeed easier, but the author wanted to demonstrate how to investigate ChoiceSelectionModel as well as how to capture oldValue in addition to newValue.

    • @vendetta3953
      @vendetta3953 7 років тому +2

      okay maybe it does, but how will I get the oldValue parameter using this method?

    • @bezcisla
      @bezcisla 7 років тому

      maybe - if you need to work with that, you have to set it as local variable - you can get that value when you use setOnMousePressed event (but you need to use 2 events now)

  • @helmiboubaker2742
    @helmiboubaker2742 3 роки тому

    i love u man (brotherly ways i am straight :p)

  • @ryanbailey1726
    @ryanbailey1726 6 років тому +2

    "v" did not work for me...I used
    getSelectionModel().selectedItemProperty().addListener( (Observable, oldValue, newValue) ->

  • @FalloutExplorer1
    @FalloutExplorer1 7 років тому

    Thanks man you're a life saver!

  • @andrewstrady4429
    @andrewstrady4429 8 років тому +3

    May be I don't understand something, but ChoiceBox has setOnAction method which does pretty much the same

  • @ransayada
    @ransayada 8 років тому +2

    hey guys sombody knows why the "newValue" in my program is an integer that described the position of the fruit? and not a String that tells which fruit you selected ?

    • @thejozo24
      @thejozo24 4 роки тому +2

      Sorry for necro, but for the fellas with the same question: there are two properties that you can get out of SelectionModel:
      selectedIndexProperty
      selectedItemProperty
      ItemProperty gives you the item in the list, IndexProperty gives you its index in the list

  • @sumitshah6157
    @sumitshah6157 9 років тому

    why I am getting double item or triple item of same value whenever I click on choice boxfor eg :if click on appleit display apple data one timeafter that if I click on manoit display mango two times

  • @MVRugby
    @MVRugby 4 роки тому

    I constantly get an error saying "Overrides Method in ChangeListener" WTF does that mean?!

    • @harshilm
      @harshilm 4 роки тому

      "ChangeListener" is an interface. Whichever class uses the interface has to implement all the methods declared in the interface. If you are using any editor with IntelliSense(VS Code, IntelliJ IDEA, Sublimes, Atom etc.) you can implement all the methods by hovering to red underline on interface's text, and select "Implement methods". Or you can google the interface for it's methods.

  • @nicklandreth2527
    @nicklandreth2527 4 роки тому

    So I thought I could make a listener that would validate text boxes character by character as they were entered similarly to a typing game. I tried the following but I get a bunch of errors and it only returns when the full length of the string has been reached.
    nameInput.textProperty().addListener((v, oldValue, newValue) -> {
    String compare = "nick";
    for(int x = 0; x

  • @Lojesad
    @Lojesad 8 років тому

    I don't understand how the lambdas here works... you pass v oldValue and newValue, how he know what kind of value he is referring to? whats v?

    • @lechi_2002
      @lechi_2002 8 років тому +2

      v is the object that the newValue and oldValue are referring to.

  • @rayli1178
    @rayli1178 7 років тому

    I got numeric value when things are selected instead of the strings. For example, if I select, "Apple" it would give me 0. Does anyone know how to fix it?
    choiceBox.getSelectionModel().selectedIndexProperty().addListener((v, oldValue, newValue) -> System.out.println(oldValue));

    • @irondeau
      @irondeau 7 років тому +2

      Change ".selectedIndexProperty()" to ".selectedItemProperty" and that should fix it.

  • @DaBeedC
    @DaBeedC 4 роки тому

    does anyone know if its possible to add a listener to say only two of the items in the selection menu?

    • @Kiroto50
      @Kiroto50 3 роки тому

      in the listener, you could validate whether the selected item is one of those two items.

  • @ECCOWAFL
    @ECCOWAFL 9 років тому

    is there a way i could save info like a users name that way when they open the GUI it will keep their name?

    • @Relseg
      @Relseg 9 років тому +1

      ***** Need to save it in a file, and make the program read that file every time it starts up and initialize whatever it needs to with the data in the file. At least that's how I'd do it.

    • @ECCOWAFL
      @ECCOWAFL 9 років тому

      Relseg Ok thanks for the help

  • @katembovictor7710
    @katembovictor7710 9 років тому

    what ide are you using?

  • @alebatata_
    @alebatata_ 8 років тому

    Im getting int values, how do i fix it? hm

    • @videostormer9354
      @videostormer9354 5 років тому +5

      i know this is 2 years too late but for newer viewers its propably cause u typed getSelectionModel().selectedIndexProperty() instead of getSelectionModel().selectedItemProperty()

  • @Pzdrs
    @Pzdrs 4 роки тому +1

    why didnt u just use choiceBox.setOnAction(event -> System.out.println("Something"));

  • @AlexTechie
    @AlexTechie 7 років тому +2

    "Bucky is not a valid variable name".

  • @alexandersage7035
    @alexandersage7035 5 років тому

    cutting out your meat consumption can cut your carbon footprint by ~30%