Home Ask Login Register

Developers Planet

Your answer is one click away!

Skizzles February 2016

How do I read the information of a JTextField from a separate class using an ActionListener

The following code is within a class that has the buttons and checkboxes defined. The problem that I am having is with the lines "String text = txtField.getText();" and "chkAccount.withdraw(amount);".

I have a separate class file that defines the txtField and am trying to get the text that has been input into that box. The error I see is "cannot find symbol". I have looked into it but cant find information in depth enough to answer my question.

The second line refers to the object chkAccount which is derived from a separate class "Account" and instantiated in the main class of my program. The error for this is the same as the above.

//Button Listeners Class
    withdrawalBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //Execute this code when the button is pressed
            if(checking.isSelected()) {
                String text = txtField.getText();
                int amount = Integer.parseInt(text);
                try {
                } catch (InsufficientFunds ex) {
                    int messageType = JOptionPane.PLAIN_MESSAGE;
                    JOptionPane.showMessageDialog(null, "Balance too low" +
                            "or amount entered not an Interval of 20.", 
                            "Notice", messageType);


AndrewIsOffline February 2016

I can't really tell what the code that you've provided is trying to do, but just based on the title of your question, I would recommend you have the class that you want the information to go to extends ActionListener, add an object of that type of class (the one that you had extend ActionListener) as an ActionListener to the JTextField, and then in the actionPerformed method (which you'll have to add, as the class now extends ActionListener) use ActionEvent.getSource (and cast) to get the JTextField! From there, you can extract the information that you need.

Post Status

Asked in February 2016
Viewed 3,519 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life