Javafx Listview Add and Edit Item

I want to add and edit the item directly to the list:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafx_test;

import java.util.Observable;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
 *
 * @author karim
 */
public class Javafx_test extends Application {

    @Override
    public void start(Stage primaryStage) {
        ObservableList<String> items = FXCollections.observableArrayList("test1", "test2");
        ListView<String> list = new ListView<>(items);

        list.setEditable(true);
        list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {

            @Override
            public ListCell<String> call(ListView<String> param) {
                return new TextFieldListCell<>(new StringConverter<String>() {

                    @Override
                    public String toString(String object) {
                        return object;
                    }

                    @Override
                    public String fromString(String string) {
                        return string;
                    }
                });
            }
        });

        Button btn = new Button();
        btn.setText("Add String");
        btn.setOnAction((ActionEvent event) -> {
            String c = new String("test");
            list.getItems().add(list.getItems().size(), c);
            list.scrollTo(c);
            list.edit(list.getItems().size() - 1);
        });

        VBox root = new VBox(list, btn);

        Scene scene = new Scene(root);

        primaryStage.setTitle("test!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Everything seems to be correct, but it doesn’t work, it looks like trying to change the first element, not just the added element in the last index, I don’t know why

+4
source share
3 answers

This is mistake.

, . , , . , , , , , . , , , , , focusModel, .

AnimationTimer ListView.edit(...) . ( , AnimationTimer handle(...), ; , .)

    btn.setOnAction((ActionEvent event) -> {
        String c = "test"+(list.getItems().size()+1);
        list.getItems().add(list.getItems().size(), c);
        list.scrollTo(list.getItems().size() - 1);
        // list.edit(list.getItems().size() - 1);

        new AnimationTimer() {

            int frameCount = 0 ;

            @Override
            public void handle(long now) {
                frameCount++ ;
                if (frameCount > 1) {        
                    list.edit(list.getItems().size() - 1);
                    stop();
                }
            }

        }.start();
    });

scrollTo(...) (, , :).

, - - , ...

+5

James_D: , . , .

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ListEdit extends Application {

    int i = 3;

    @Override
    public void start(Stage primaryStage) {
        ObservableList<String> items = FXCollections.observableArrayList("test1", "test2");
        ListView<String> list = new ListView<>(items);
        list.setCellFactory(TextFieldListCell.forListView());
        list.setEditable(true);

        Button btn = new Button();
        btn.setText("Add String");
        btn.setOnAction((ActionEvent event) -> {
            list.getItems().add(i - 1, "test" + i);
            list.edit(i - 2);
            i++;
        });

        VBox root = new VBox(list, btn);

        Scene scene = new Scene(root);

        primaryStage.setTitle("test!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

application

+4

The problem is that it Celldoes not update before the call edit. Since cell updates are performed during layout, a call layoutbefore editing should fix the problem:

Example:

@Override
public void start(Stage primaryStage) {
    ListView<String> listView = new ListView<>();
    listView.setEditable(true);
    listView.setCellFactory(TextFieldListCell.forListView());
    Button editButton = new Button("Add & Edit");
    editButton.setOnAction((ActionEvent event) -> {
        listView.getItems().add("");
        listView.scrollTo(listView.getItems().size() - 1);
        listView.layout();
        listView.edit(listView.getItems().size() - 1);
    });

    Scene scene = new Scene(new VBox(listView, editButton));

    primaryStage.setScene(scene);
    primaryStage.show();
}
+4
source

Source: https://habr.com/ru/post/1608348/


All Articles