-
-
Save sedj601/41c6106bc9d304f787f0a2023b824af5 to your computer and use it in GitHub Desktop.
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); | |
} | |
} |
JavaFX
is open, so you can write you own ListView
and write things to work how you think they should.
Have fun coding!
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.
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.
Fuck. Why you tell me, what i need or what i'm not need.
I delete my question, good luck !
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.
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.
Good luck coding. 😀
With pleasure. Without stupid answers.
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.
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.