Search code examples
javainputjtextareakeylistenerjwindow

Getting Input from JTextArea


public static void main(String[] args) throws PrinterException {

    Toolkit tk = Toolkit.getDefaultToolkit();
    int xSize = ((int) tk.getScreenSize().getWidth());
    int ySize = ((int) tk.getScreenSize().getHeight());
    final String password = "Alphabet";

    JFrame screen = new JFrame("INSERT TITLE HERE");

    screen.setSize(xSize, ySize);
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.setResizable(false);

    screen.setVisible(true);

    final JWindow window = new JWindow(screen);
    window.setSize(xSize, ySize);
    window.setName("INSERT TITLE HERE");

    final JTextArea text = new JTextArea();
    text.setText("Type the password > ");
    text.setBackground(Color.BLACK);
    text.setForeground(Color.green);



    window.add(text);
    window.setVisible(true);

    text.addKeyListener(new java.awt.event.KeyAdapter(){
        public void keyReleased(java.awt.event.KeyEvent evt) {
            System.out.println(evt.getKeyCode());

            if(evt.getKeyCode() == 51){
                System.out.println(text.getText());
                String passAttempt = text.getText();
                int start = passAttempt.indexOf('>') + 2 ;
                int end = passAttempt.indexOf('#');
                passAttempt = passAttempt.substring(start, end);
                if(passAttempt.equals(password)) {
                    System.out.println("SUCCESSFUL");
                    text.setText("Login Successful");
                    window.add(text);
                    window.setVisible(true);
                    }
                if(!passAttempt.equals(password)) {
                    System.out.println(passAttempt);
                    text.setText("Incorrect");
                    window.add(text);
                    window.setVisible(true);                        
                } 
                }
    }

    });
}

I'm trying to create a fallout-esque user interface, and I need to get user input to type in a 'password' before I open up the UI, but I can't figure out how to read input from the JTextArea, please help!

NOTE: The main goal here is to keep the feel of using an old school DOS program, so i can't use JOptionPane or anything like that.

EDIT: Thanks for all your help everybody, I ended up going with the keyListener, and it worked perfectly!


Solution

  • Since you use JTextArea instead of JPasswordField, you have to filter out the password from the content of the text from the your JTextArea. The idea that I have so far is to make a condition to capture the password after the Type the password > sentences.

    Then, save the original password somewhere in ArrayList and masking the original password to something else like *** and replace the original password content on the JTextArea to the masked password. Maybe this is not a perfect solution for your question, but I believe this answer can help you at least.

    public class Test {
    private static String password;
    private static List<String> passwordList;
    
    public static void main(String[] args) throws PrinterException {
    
        keyEvents ke = new keyEvents();
        Toolkit tk = Toolkit.getDefaultToolkit();
        int xSize = ((int) tk.getScreenSize().getWidth());
        int ySize = ((int) tk.getScreenSize().getHeight());
    
        JFrame screen = new JFrame("INSERT TITLE HERE");
    
        screen.setSize(xSize, ySize);
        screen.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        screen.setResizable(false);
    
        screen.setVisible(true);
    
        JWindow window = new JWindow(screen);
        window.setSize(xSize, ySize);
        window.setName("INSERT TITLE HERE");
    
        final JTextArea text = new JTextArea();
        text.setText("Type the password > ");
        text.setBackground(Color.BLACK);
        text.setForeground(Color.green);
    
        passwordList = new ArrayList<String>();
    
        text.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                String[] content = text.getText().split("\n");
                String newContent = "";
    
                for (int i = 0; i < content.length; i++) {
                    if (content[i].contains("Type the password > ")) {
                        password = content[i].replace("Type the password > ", "");
    
                        if(password.length() > 0){
                            passwordList.add(password.substring(password.length() - 1));
                        }
    
                        content[i] = "Type the password > " + passwordMasked(password);
    
    
                    }
    
                    newContent += content[i];
                }
    
                if (evt.getKeyCode() == 10) {
                    newContent += "\nYour password is " + Arrays.toString(passwordList.toArray());
                }
    
                text.setText(newContent);
            }
        });
    
        window.add(text);
        window.setVisible(true);
    
    }
    
    public static String passwordMasked(String password) {
        String value = password;
        password = "";
        for (char c : value.toCharArray()) {
            password += "*";
        }
    
        return password;
    }