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!
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.
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.
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)
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 ?
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
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
"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.
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
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));
***** 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.
i know this is 2 years too late but for newer viewers its propably cause u typed getSelectionModel().selectedIndexProperty() instead of getSelectionModel().selectedItemProperty()
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!
Update: setOnAction was added from 8.
Thanks!
You actually make javafx really easy!!!
you are awesome my friend!!! God Bless You!!!
Thank you sooooo much I was searching for hours to get how to implement the listener
You are the most awesome teacher i have ever seen !!!!! God bless you
You safed my day! Thanks a lot!
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();
}
}
It Works. So Why Did He Make An addListener ?
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.
Thanks a lot, you're life savior :)
Thank you again!
Awesome video.
making your custom listener is awesome instead let Sun Microsoft develop everything for us.
The whole video is about one line of code xd.
P.S. I like your videos!
Very useful tutorial. Thanks for sharing knowledge.
choiceBoxs.setOnAction(e -> System.out.println(colors.getValue()));
It's much easier this way.
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.
okay maybe it does, but how will I get the oldValue parameter using this method?
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)
i love u man (brotherly ways i am straight :p)
"v" did not work for me...I used
getSelectionModel().selectedItemProperty().addListener( (Observable, oldValue, newValue) ->
Thanks man you're a life saver!
May be I don't understand something, but ChoiceBox has setOnAction method which does pretty much the same
+Andrew Strady you are right.
Yes i don't understand either
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 ?
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
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
I constantly get an error saying "Overrides Method in ChangeListener" WTF does that mean?!
"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.
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
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?
v is the object that the newValue and oldValue are referring to.
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));
Change ".selectedIndexProperty()" to ".selectedItemProperty" and that should fix it.
does anyone know if its possible to add a listener to say only two of the items in the selection menu?
in the listener, you could validate whether the selected item is one of those two items.
is there a way i could save info like a users name that way when they open the GUI it will keep their name?
***** 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.
Relseg Ok thanks for the help
what ide are you using?
+katembo victor he's using intellij
ok,thanks
Im getting int values, how do i fix it? hm
i know this is 2 years too late but for newer viewers its propably cause u typed getSelectionModel().selectedIndexProperty() instead of getSelectionModel().selectedItemProperty()
why didnt u just use choiceBox.setOnAction(event -> System.out.println("Something"));
Same Question
"Bucky is not a valid variable name".
cutting out your meat consumption can cut your carbon footprint by ~30%