2013-05-13 30 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());