Search code examples
javanetbeansjtextfield

How can i pass jTextField value from one class to another when clicking a jButton?


Greetings from jamaica,

I'm having trouble with a netbeans java project. I have two separate classes: A gui class called MainJFrame.java and another class called main.java.

In the MainJFrame form i have a jTextField and a jbutton. How can i pass the jtextfield value to the main.java class by clicking the jbutton?

thanks.


Solution

    1. You keep a reference to an instance of your Main class in the MainJFrame class.
    2. You define an ActionListener for your JButton.
    3. In this ActionListener, you get the value of the JTextField with JTextField.getText()
    4. And you call a method of Main class on the instance you kept in first step with the text as a parameter.