import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import java.awt.event.ActionEvent;

public class PasswordSelectionWordTest {
    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeAndWait(() -> {
            String str = "one two three";
            JPasswordField field = new JPasswordField(str);

            boolean foundSuspiciousInputs = false;
            for (int condition : new int[] {
                    JComponent.WHEN_IN_FOCUSED_WINDOW,
                    JComponent.WHEN_FOCUSED,
                    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
            }) {
                InputMap inputMap = field.getInputMap(condition);
                if (inputMap.allKeys() == null)
                    continue;
                for (KeyStroke keyStroke : inputMap.allKeys()) {
                    Object actionBinding = inputMap.get(keyStroke);
                    if (String.valueOf(actionBinding).contains("word")) {
                        System.out.println(inputMap.get(keyStroke) + " (" + keyStroke + ")");
                        foundSuspiciousInputs = true;
                    }
                }
            }

            if (!foundSuspiciousInputs) {
                System.out.println("No suspicious KeyStrokes detected; this test passes.");
                System.exit(0);
            }
            System.out.println("\n\nTry using the above KeyStrokes to identify the boundary of words in the JPasswordField");


            JFrame f = new JFrame();
            f.getContentPane().add(field);
            f.pack();
            f.setVisible(true);
        });
    }
}
