Password in JTextArea

后端 未结 4 1521
无人及你
无人及你 2021-01-24 18:50

Is there any way using JTextArea to hide the text when user types??

Kind of like password..

in JTextArea i have..

passw

4条回答
  •  心在旅途
    2021-01-24 19:30

    Here's what I was talking about in comments. Do note that this is just a quick example (might wanna store the password securely, etc.), but should get you started.

    This code simply puts a DocumentFilter on the JTextArea and never allows password characters to be put into it's Document. Instead they are re-routed somewhere else. This is similar behavior to that of a console expecting sensitive input.

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JToggleButton;
    import javax.swing.SwingUtilities;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DocumentFilter;
    import javax.swing.text.PlainDocument;
    
    public class CapturePassword extends JFrame {
    
        private JScrollPane scroll;
        private JTextArea textArea;
        private JToggleButton expectPassword;
        private StringBuilder password; // you would of course use something else
    
        public CapturePassword() {
            setLayout(new BorderLayout());
    
            password = new StringBuilder();
    
            textArea = new JTextArea();
            scroll = new JScrollPane(textArea);
            add(scroll);
    
            expectPassword = new JToggleButton("Capture password");
            add(expectPassword, BorderLayout.PAGE_END);
    
            expectPassword.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    if (expectPassword.isSelected()) {
                        capture(true);
                    } else {
                        capture(false);
                        JOptionPane.showConfirmDialog(
                                CapturePassword.this, 
                                "Captured password: " + password.toString(), 
                                "Password!", JOptionPane.DEFAULT_OPTION, 
                                JOptionPane.INFORMATION_MESSAGE);
                        password.setLength(0); // reset
                    }
                    textArea.requestFocusInWindow();
                }
            });
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(200, 400);
            setLocationRelativeTo(null);
        }
    
        private void capture(boolean start) {
            PlainDocument document = (PlainDocument)textArea.getDocument();
            DocumentFilter filter = new DocumentFilter() {
    
                private void doAppend(String text) {
                    if (text.endsWith("\n")) {
                        expectPassword.doClick();
                    } else {
                        password.append(text);
                    }
                }
    
                @Override
                public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
                    // you would have to handle multi-line pastes here also
                    doAppend(text);
                }
    
                @Override
                public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
                    // cannot remove while filtering
                }
    
                @Override
                public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                    doAppend(text);
                }          
            };
            document.setDocumentFilter(start ? filter : null);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    new CapturePassword().setVisible(true);
                }
            });
        }
    
    }
    

提交回复
热议问题