2013-05-13 101 views
0

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

+0

为什么不只是你哈希密码,以便您无需在显示密码时使用密码掩码。 – AsirC 2013-05-13 13:58:50

回答

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。

希望它有帮助。

0

我认为,一个标签可以做的更好。(总部设在@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());