I am currently working on an open source code for a Matrix calculator. What I am trying to achieve is to get user to enter numbers only into the JTextArea. Numbers include negative numbers and also decimal numbers. My questions are as follows:
- How do I restrict user inputs into the JTextArea. Inputs that the JTextArea should accept are: numbers (0-9), space, decimal point. For example, if user presses the letter 'a', the letter does not appear in the JTextArea and also he/she hears a warning sound. If the user presses a number e.g '2'it is added into the JTextArea.
- Or there is no solution to my first question; this on is my alternate method. How do I add an actionListener to a JTextArea. The Action Listener will be used to validate user inputs into the JTextArea. I am working with Matrices here and I would like users to input numbers only into the text area, when users press letters or other symbols, the program prompts the user that the input is invalid and that the user must enter numbers only. If there is a solution for this one, I would also like to go one step ahead to check if the numbers entered represents a valid matrix (number of columns; i.e. numbers saperated by a space; must be equal in all rows).
Snapshot of the Matrix Calculator showing letters entered into the JTextArea
I have not yet done any coding on this yet as I am not sure how to add an action listener to my JTextArea.