Affichage des articles dont le libellé est Hide Password In Java. Afficher tous les articles
Affichage des articles dont le libellé est Hide Password In Java. Afficher tous les articles

Java - Show And Hide Passwords in JTable

How to Show And Hide Password Inside a JTable Cell In Java Netbeans

How to Show And Hide Password Inside a JTable Cell In Java Netbeans


In this Java Tutorial we will see How To Create a JTable allowing the user to toggle the visibility of passwords using a checkbox in the "Show-Password" column. In Java Using Netbeans.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package new_tutorials;

import java.awt.Component;
import java.awt.Font;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author 1BestCsharp
 */
public class PasswordTableFrame extends JFrame{

    public PasswordTableFrame(){
        
        setTitle("Password Table Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600,400);
        setLocationRelativeTo(null);
        
        initializeUI();
        
    }
    
    private void initializeUI(){
        // Create a table with a custom table model
        JTable table = new JTable(new PasswordTableModel());
        
        // Set custom cell renderer and editor for the "Show Password" column
        table.getColumnModel().getColumn(2).setCellRenderer(new CheckBoxRenderer());
        table.getColumnModel().getColumn(2).setCellEditor(new CheckboxEditor());
        
        table.setFont(new Font("Arial",Font.PLAIN,22));
        table.setRowHeight(50);
        
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }
    
    public static void main(String[] args) {
        PasswordTableFrame frame = new PasswordTableFrame();
        frame.setVisible(true);
    }
    
    // Create a custom table model to handle password display
    private class PasswordTableModel extends AbstractTableModel{

        private final String[] columnNames = {"Username","Password","Show-Password"};
        private final Object[][] data = {
                {"user1", "password1", false},
                {"user2", "password2", false},
                {"user3", "password3", false},
                {"user4", "password4", false},
                {"user5", "password5", false}
        };
        
        @Override
        public int getRowCount() { return data.length; }

        @Override
        public int getColumnCount() { return columnNames.length; }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
        // Hide the password if "Show Password" is unchecked
            if(columnIndex == 1 && ! (boolean) data[rowIndex][2]){
                return "**********";
            }
            
            else{ return data[rowIndex][columnIndex]; }
            
        }
        
        @Override
        public String getColumnName(int columnIndex){
            return columnNames[columnIndex];
        }
        
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex){
            return columnIndex == 2;
        }
        
        
        @Override
        public void setValueAt(Object value, int rowIndex, int columnIndex){
            // Update the "Password" column when "Show Password" checkbox is modified
            if(columnIndex == 2){
                data[rowIndex][columnIndex] = value;
                fireTableCellUpdated(rowIndex, columnIndex - 1);
            }
        }
        
    }
    
    
    // Create a custom cell renderer for the checkbox
    private class CheckBoxRenderer extends DefaultTableCellRenderer{
        private final JCheckBox checkBox = new JCheckBox();
        
        public CheckBoxRenderer(){
         
            checkBox.setHorizontalAlignment(SwingConstants.CENTER);
            
        }
        
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean  isSelected, boolean hasFocus, int row, int column){
            
            checkBox.setSelected((boolean)value);
            return checkBox;
            
        }
    }
    
    
    // Create a custom cell editor for the checkbox
    private class CheckboxEditor extends AbstractCellEditor implements TableCellEditor{

        private final JCheckBox checkBox = new JCheckBox();
        
        public CheckboxEditor(){
            checkBox.addActionListener(e -> stopCellEditing());
            checkBox.setHorizontalAlignment(SwingConstants.CENTER);
        }
        
        @Override
        public Object getCellEditorValue() {
            
            return checkBox.isSelected();
            
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        
            checkBox.setSelected((boolean) value);
            return checkBox;
            
        }
        
    }
  
}


The Final Result:

Password Inside a JTable Cell In Java

Show Password Inside a JTable Cell In Java

Show And Hide Password Inside a JTable Cell In Java