簡體   English   中英

用TextField替換選定的Label

[英]Replace a selected Label with a TextField

我已經使用創建了LabelListView

ListView<Label> list = new ListView<Label>();
Image folder = new Image(getClass().getResourceAsStream("folder.png"));
ObservableList<Label> data = FXCollections.observableArrayList();   
for (int i = 0; i < 6; i++) {
    Label lbl = new Label();
    lbl.setText("label" + i);
    lbl.setGraphic(new ImageView(folder));
    lbl.setContentDisplay(ContentDisplay.LEFT); 
    lbl.setGraphicTextGap(10.2); 
    data.add(lbl);              
}
list.setItems(data);

我希望用戶能夠雙擊ListView內的任何Label ,所選的Label應該替換為TextField以便用戶可以動態輸入新的標簽名稱。

用戶按下Enter鍵后TextField應該重新變成Label

不要將Label用作ListView數據類型。 使用String 然后,您可以只使用標准的TextFieldListCell ,它具有您所描述的功能。 由於您希望在標准單元格顯示中顯示圖形,因此僅在不顯示文本字段時就繼承TextFieldListCell並覆蓋適當的方法以包括圖形:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import javafx.util.converter.DefaultStringConverter;

public class EditableListViewTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        ListView<String> list = new ListView<>();
        Image testImg = new Rectangle(12, 12, Color.CORNFLOWERBLUE).snapshot(null, null);
        for (int i = 0; i < 6; i++) {
            list.getItems().add("label "+i);
        }

        StringConverter<String> identityStringConverter = new DefaultStringConverter();

        list.setCellFactory(lv -> new TextFieldListCell<String>(identityStringConverter) {

            private ImageView imageView = new ImageView(testImg);


            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (! empty && ! isEditing()) {
                    setStaticGraphic();
                }
            }

            @Override
            public void cancelEdit() {
                super.cancelEdit();
                setStaticGraphic();
            }

            @Override
            public void commitEdit(String newValue) {
                super.commitEdit(newValue);
                setStaticGraphic();
            }

            private void setStaticGraphic() {
                setGraphic(imageView);
                setContentDisplay(ContentDisplay.LEFT);
                setGraphicTextGap(10.2);                
            }
        });

        list.setEditable(true);

        primaryStage.setScene(new Scene(new BorderPane(list), 250, 400));
        primaryStage.show();
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM