0

在这里,我正在创建一个 TableView 并通过传递 ObservableList 在该 TableView 中显示数据。这里的可观察列表取自数据库。并显示在名称-值列的表视图中。这里数据中也存在一些密码。那么,如何对用户隐藏这些密码字段?

4

4 回答 4

1

我认为标签可以做得更好..(基于@Antonio J. 回复)

public class PasswordLabelCell extends TableCell<YourTableBean, String> {
    private Label label;

    public PasswordLabelCell() {
        label = new Label();
        this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        this.setGraphic(null);
    }

    private String genDotString(int len) {
        String dots = "";

        for (int i = 0; i < len; i++) {
            dots += "\u2022";
        }

        return dots;
    }

    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (!empty) {
            label.setText(genDotString(item.length()));
            setGraphic(label);
        } else {
            setGraphic(null);
        }
    }
}

现在设置列单元工厂

myPasswordColumn.setCellFactory(param -> new PasswordLabelCell());
于 2015-01-29T21:45:10.570 回答
1

您可以创建一个带有 PasswordField 的单元工厂。

首先,您必须设置电池工厂。

tableColumnPass.setCellFactory(new Callback<TableColumn<YourTableBean,String>, TableCell<YourTableBean,String>>() {         
        @Override
        public TableCell<YourTableBean, String> call(TableColumn<YourTableBean, String> cell) {
            return new PasswordFieldCell();
        }
    });

在您的 PasswordFieldCell 中,您可以使用下一个代码,在单元格的图形中设置更新项方法中的密码字段。

public class PasswordFieldCell extends TableCell<YourTableBean, String> {
private PasswordField passwordField;    
public PasswordFieldCell() {
    passwordField = new PasswordField();
    passwordField.setEditable(false);
    this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    this.setGraphic(null);
}

@Override
protected void updateItem(String item, boolean empty) {
    super.updateItem(item, empty);
    if(!isEmpty()){
        passwordField.setText(item);
        setGraphic(passwordField);
    }else{
        setGraphic(null);
    }
}}

如果你没有 Bean,你仍然可以这样做。只需要更改特定列的cellfactory。

希望能帮助到你。

于 2013-05-13T14:48:52.337 回答
1

我为支持编辑的密码单元做好了准备使用类。

import javafx.geometry.Pos;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.input.KeyCode;
import javafx.scene.text.TextAlignment;
import javafx.util.Callback;
import javafx.util.StringConverter;
import javafx.util.converter.DefaultStringConverter;

/**
 *
 * @author Rami Manaf Abdullah
 */
public class PasswordFieldTableCell<S, T> extends TableCell<S, T> {

    public static <S> Callback<TableColumn<S, String>, TableCell<S, String>> forTableColumn() {
        return forTableColumn(new DefaultStringConverter());
    }

    public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> forTableColumn(StringConverter<T> converter) {
        return (param) -> {
            return new PasswordFieldTableCell<S, T>(converter);
        };
    }

    private StringConverter<T> converter;
    private PasswordField passwordField;
    private Label text;
    
    public PasswordFieldTableCell(StringConverter<T> converter) {
        super();
        this.converter = converter;
        passwordField = new PasswordField();
        passwordField.setAlignment(Pos.CENTER);
        passwordField.setOnAction(event -> {
            commitEdit(converter.fromString(passwordField.getText()));
            event.consume();
        });
        passwordField.setOnKeyReleased(t -> {
            if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
                t.consume();
            }
        });
        text = new Label();
        text.setTextAlignment(TextAlignment.CENTER);
        text.setAlignment(Pos.CENTER);
        text.prefWidthProperty().bind(widthProperty());
        this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        this.setGraphic(null);
    }

    @Override
    protected void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            setGraphic(null);
        } else {
            if (isEditing()) {
                passwordField.setText(item.toString());
                setGraphic(passwordField);
            } else {
                StringBuilder builder = new StringBuilder(item.toString().length());
                for (int i = 0; i < item.toString().length(); i++) {
                    builder.append('\u2022');
                }
                text.setText(builder.toString());
                setGraphic(text);
            }
        }
    }

    @Override
    public void startEdit() {
        if (!isEditable()
                || !getTableView().isEditable()
                || !getTableColumn().isEditable()) {
            return;
        }
        super.startEdit();

        if (isEditing()) {
            passwordField.setText(converter.toString(getItem()));
            setGraphic(passwordField);
            passwordField.selectAll();
            passwordField.requestFocus();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void cancelEdit() {
        super.cancelEdit();
        setGraphic(text);
    }
}
于 2021-02-08T19:54:34.570 回答
0

我在寻找解决方案时访问了这个。但是,建议的方法没有显示如何更新该字段。

我的解决方案很简单。使用文本字段。在填充 ObservableList 时,只需将文本替换为 ******

于 2019-08-01T18:47:31.160 回答