Skip to content

Instantly share code, notes, and snippets.

@sedj601
Created February 17, 2023 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sedj601/41c6106bc9d304f787f0a2023b824af5 to your computer and use it in GitHub Desktop.
Save sedj601/41c6106bc9d304f787f0a2023b824af5 to your computer and use it in GitHub Desktop.
Update ListView by Double Clicking on Cell
import java.util.Optional;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class App extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("List View Sample");
ObservableList<Person> people = FXCollections.observableArrayList();
ListView<Person> listView = new ListView(people);
listView.setCellFactory((p) -> new ListCell<>() {
@Override
public void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
if (empty || person == null) {
setText(null);
} else {
setText(person.getName());
}
}
});
listView.setOnMouseClicked((MouseEvent click) -> {
if (click.getClickCount() == 2) {
//Use ListView's getSelected Item
Person tempPerson = listView.getSelectionModel().getSelectedItem();
int index = listView.getSelectionModel().getSelectedIndex();
TextInputDialog dialog = new TextInputDialog(tempPerson.getName());
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter your name:");
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
tempPerson.setName(result.get());
listView.getItems().set(index, tempPerson);
}
}
});
Person person1 = new Person("Tom");
Person person2 = new Person("Kim");
people.add(person1);
people.add(person2);
VBox root = new VBox(listView);
primaryStage.setScene(new Scene(root, 200, 250));
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
@james-d
Copy link

james-d commented Feb 17, 2023

This code only works by virtue of the current implementation of ModifiableObservableListBase. Specifically, the current implementation fires a list change event (which is received by the list view) every time set(index, element) is called, even if that call does not actually change the element. It would be completely consistent with the API specification for the set(index, element) method to test if Objects.equals(get(index), element) and not fire the list change event if that were true (because it's not actually a change). The Property classes are implemented this way. In that case your list view would not update. So your code could be broken by a future release of JavaFX. I don't know how likely that is, but you should be aware that this is coding to a specific implementation, not to the API specificiation.

@james-d
Copy link

james-d commented Feb 17, 2023

I don't believe there is a valid reason not to use StringProperty (at least in the presentation tier of your application). This is the way the API is intended to be used. The memory overhead is minimal and there is essentially no CPU overhead. If you profile your application and it reveals otherwise, then either you are using it the wrong way, or there is a bug in the JavaFX library code, which you should report.

@sedj601
Copy link
Author

sedj601 commented Feb 17, 2023

I don't know if you have any other choice given the restriction you placed on yourself, or that is placed on you by a company.

@sedj601
Copy link
Author

sedj601 commented Feb 17, 2023

To me, it's simple. You and everyone around you need to go into this with the understanding that if you have the given restriction, there is a chance your application may be broken in the future. So y'all can go with that understanding or lift the restriction and do things correctly.

@sedj601
Copy link
Author

sedj601 commented Feb 17, 2023

JavaFX is open, so you can write you own ListView and write things to work how you think they should.
Have fun coding!

@james-d
Copy link

james-d commented Feb 17, 2023

OK, so don't change the Person object, but create a new class, I don't know, ObservablePerson, (using StringProperty for the name) and use that in the backing list for your ListView. Translate between Person and ObservablePerson objects at the boundary between the presentation tier and the business tier of the application. I don't know if that solves your problem, because I don't know the reasons for that restriction, but if you use JavaFX for your presentation tier, if you don't use the JavaFX property classes you are fighting against the API instead of using it.

@sedj601
Copy link
Author

sedj601 commented Feb 18, 2023

You have to learn JavaFX and abandon Android while you learn. Their coding styles are very different. I did the same thing when I learned JavaFX. Forget everything Android and learn how to do things as they were designed. There is a lookup method in JavaFX, but just like your last issue, you should not have to use it.

@ilwsm
Copy link

ilwsm commented Feb 18, 2023

Fuck. Why you tell me, what i need or what i'm not need.
I delete my question, good luck !

@sedj601
Copy link
Author

sedj601 commented Feb 18, 2023

Get out of your feelings and learn to listen. You literally are getting JavaFX advice from some of the best JavaFX people in the world. I am not included in that group. Yet, you have convinced yourself to go against their advice. It getting comical at this point.

@ilwsm
Copy link

ilwsm commented Feb 18, 2023

You have no idea what I want to do and advise me not to do it. This is bullshit. You could delve into the topic before giving me advice without answering direct questions. I absolutely do not accept this style of communication.
I have enough experience to know how to respond to such answers.

@sedj601
Copy link
Author

sedj601 commented Feb 18, 2023

Good luck coding. 😀

@ilwsm
Copy link

ilwsm commented Feb 18, 2023

With pleasure. Without stupid answers.

@kleopatra
Copy link

kleopatra commented Feb 19, 2023

OMG .. what a jerk - personally I'm certainly favoring any amateur (houseperson or not) eager to learn over a knows-it-all self proclaimed professional. At the end, the latter will also learn, most probably the hard way sitting on a jumble of brittle broken code.

As to this way around: also working for tableView, probably since fixing a bug in TableCell which prevented a cell update If the old row value was the same as the new.

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