Home Java Example Java Swing Get JTextField value from another class



Get JTextField value from another class
Posted on: October 15, 2009 at 12:00 AM
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

 

Related Tags for Get JTextField value from another class:
javacclassormtextformfunctiontextboxbuttonfunioclassesgetvaluethiscreatedefinetexforuncboxtosseexteilpefrominrmasmjclesfuncalldefinedwhichsssacreatedatishalleaandsaxtvatttwsstbothavaluhatfinndonomo


More Tutorials from this section

Ask Questions?    Discuss: Get JTextField value from another class  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.