[an error occurred while processing this directive]

Get JTextField value from another class

                         

In this section, you will learn how to get the textfield value from other class. For this, we have created two classes ClassA.java and ClassB.java. In ClassA, we have defined a textbox 'text1' that will get the value from the textbox 'text' that is defined in  ClassB.java. We have defined a button in ClassB which will perform all this function. 

Here is the code of ClassA.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ClassA{
JFrame frame;
JTextField text1;
void form()
{
frame=new JFrame("test");
JPanel cp=new JPanel();
text1=new JTextField(10);
cp.add(text1);
frame.add(cp);
frame.setSize(300,100);
frame.setVisible(true);
}

Here is the code of ClassB.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ClassB implements ActionListener
{
JFrame f;
JTextField text;
JButton jb;
ClassA m;
public static void main(String args[])
{
ClassB s=new ClassB();
s.form();
}
void form()
{
  text=new JTextField(10);
jb=new JButton("Submit");
f=new JFrame("test");
JPanel cp=new JPanel();
cp.add(text);
cp.add(jb);

jb.addActionListener(this);

f.setSize(300,100);
f.setVisible(true);
f.add(cp);
}
public void actionPerformed(ActionEvent ae)
{
m=new ClassA();
m.form();
m.text1.setText(text.getText());
}
}

Download Source Code

 

                         

[an error occurred while processing this directive]