Home Java Example Java Swing Get JTextField value from another class

Related Tutorials


 
 

Share on Google+Share on Google+

Get JTextField value from another class

Advertisement
In this section, you will learn how to get the textfield value from other class.

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

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 15, 2009

Related Tutorials

Discuss: Get JTextField value from another class  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com