swing text field problem
I want to navigate the content of text field from one class to another and viceversa through button but when I am getting the text from first class I am getting null value.please help me..here is the code
import javax.swing.*;
import java.awt.event.*;
class Frame1 extends JFrame
{
private JButton btn1;
private JTextField tf1;
public Frame1()
{
setLayout(null);
setSize(500,500);
tf1=new JTextField();
tf1.setBounds(100,100,100,30);
btn1=new JButton("click for second frame");
btn1.setBounds(100,250,100,30);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String text=tf1.getText();
new Frame2(text);
}
});
add(tf1);
add(btn1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public JTextField getTextField1()
{
return tf1;
}
}
class Frame2 extends JFrame
{
private JButton btn2;
private JTextField tf2;
private String secondtext;
public Frame2()
{
}
public Frame2(String firsttext)
{
setLayout(null);
setSize(500,500);
tf2=new JTextField();
tf2.setText(firsttext);
secondtext=tf2.getText();
tf2.setBounds(100,100,100,30);
btn2=new JButton("click for first frame");
btn2.setBounds(100,250,100,30);
btn2.addActionListener(new MyActionListener());
add(tf2);
add(btn2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public JTextField getTextField2()
{
return tf2;
}
public String getText()
{
return secondtext;
}
}
class MyActionListener extends Frame1 implements ActionListener
{
private String text;
private JTextField tf1;
private JTextField tf2;
private Frame2 frame2;
public MyActionListener()
{
super();
frame2=new Frame2();
text=frame2.getText();
modify();
}
public void actionPerformed(ActionEvent e)
{
new MyActionListener();
}
public void modify()
{
tf1=super.getTextField1();
tf2=frame2.getTextField2();
tf1.setText(text);
}
}
class MyFrame
{
public static void main(String args[])
{
new Frame1();
}
}
View Answers
June 10, 2013 at 5:13 PM
try to do like the following :
import javax.swing.*;
import java.awt.event.*;
class Frame1 extends JFrame
{
private JButton btn1;
private JTextField tf1;
public Frame1()
{
setLayout(null);
setSize(500,500);
tf1=new JTextField();
tf1.setBounds(100,100,100,30);
btn1=new JButton("click for second frame");
btn1.setBounds(100,250,200,30);
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text=tf1.getText();
new Frame2(text);
}
});
add(tf1);
add(btn1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public Frame1(String text1)
{
setLayout(null);
setSize(500,500);
tf1=new JTextField();
tf1.setText(text1);
tf1.setBounds(100,100,100,30);
btn1=new JButton("click for second frame");
btn1.setBounds(100,250,200,30);
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text=tf1.getText();
new Frame2(text);
}
});
add(tf1);
add(btn1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public JTextField getTextField1()
{
return tf1;
}
}
Continue...
June 10, 2013 at 5:14 PM
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Frame2 extends JFrame {
private JButton btn2;
private JTextField tf2;
private String secondtext;
private Frame1 frame1;
public Frame2()
{
}
public Frame2(String firsttext)
{
setLayout(null);
setSize(500,500);
tf2=new JTextField();
tf2.setText(firsttext);
//secondtext=tf2.getText();
tf2.setBounds(100,100,100,30);
btn2=new JButton("click for first frame");
btn2.setBounds(100,250,200,30);
//btn2.addActionListener(new MyActionListener());
btn2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
secondtext=tf2.getText();
frame1 = new Frame1(secondtext);
}
});
add(tf2);
add(btn2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public JTextField getTextField2()
{
return tf2;
}
public String getSecondText()
{
return secondtext;
}
}
Continue...
June 10, 2013 at 5:15 PM
class MyFrame
{
public static void main(String args[])
{
new Frame1();
}
}
Thanks.
Ads
Related Tutorials/Questions & Answers:
swing text field problem
swing text field problem I want to navigate the content of
text field from one class to another and viceversa through button but when I am getting the
text from first class I am getting null value.please help me..here
swing text field problem
swing text field problem I want to navigate the content of
text field from one class to another and viceversa through button but when I am getting the
text from first class I am getting null value.please help me..here
Advertisements
Swing Problem on submission of button
Swing Problem on submission of button How to close current frame (Frame1) and open a new frame (Frame2) already created and pass the data to frame2 from frame1 on the clicking of button in
swing
problem in swing-awt
problem in
swing-awt I am doing project in core java and i am facing one
problem.
I have to add command prompt(terminal) to the fream at bottom but not getting the solution.
So please help me.
thanks and regards,
Aakash
jdbc and swing problem in netbeans
jdbc and
swing problem in netbeans i reteived the table from database in a jdbc program.
next i want to do is place the table as it is in a jpanel.. i am using netbeans IDE
can u tel me how to do that one?? urgent
jdbc and swing problem in netbeans
jdbc and
swing problem in netbeans i reteived the table from database in a jdbc program.
next i want to do is place the table as it is in a jpanel.. i am using netbeans IDE
can u tel me how to do that one?? urgent
CALULATION IN TEXT FIELD
CALULATION IN
TEXT FIELD Suppose 3
text Field first amount ,second amount and third sum of these amount,this amount will appear when tab in next filed(i don't want use any type button) in java
swing HOW? PLEASE HELP
Text field save as word file
Text field save as word file Dear experts At run how to save set of
text field contains
text into single Word file. To save in our desktop computer. Solve my
problem
converting field to text sql
converting
field to
text sql I wanted to convert the
field to
TEXT in SQL.. is it possible?
SQL - Converting the
field to
TEXT works... the
field to
TEXT works
SELECT CONVERT(
TEXT,fld_name) FROM TABLE_NAME
validate text field iPhone
validate
text field iPhone i got two different UITextfield in my iPhone application for user login. Just wondering how to validate
Digit Only text field
Digit Only
text field How should we accept digits only in a textbox?
(using JSTL only, without Javascript
clearing text field in spring form
clearing
text field in spring form hi,
I have spring form in that i have security question dropdown
field and security answer
text field ,when i submitted form with errors the answers
text field value should be clear, for this i
Text Field in HTML
Text Field in HTML
The
Text Field in HTML are one line areas, which enable the user...
Text Field in HTML.In this Tutorial, the
code create a
text field in HTML page
JSP combo and text field related questio
JSP combo and
text field related questio in JSP,i had stored data from database in a textboxes usin combo..now i want to use these values of
text... and name given to that
text field..but its showing somethimng null pointer exception
How to Define Text Field Validation iPhone
How to Define
Text Field Validation iPhone Hi,
Can anybody explain me how to define textfield validation iPhone program. please phone online help reference or example.
Thanks
fetch values from database into text field
fetch values from database into
text field please provide the example for fetching values from database into
text field of table
as if i am... "rs.getString" in the
text field also.. import java.awt.*;
import
fetch values from database into text field
fetch values from database into
text field please provide the example for fetching values from database into
text field of table
wth edit...;/td>");
the i am getting "rs.getString" in the
text field also..  
Use of Text Field
Use of
Text Field,
Text box,
Text Area, Checkbox,Dropdownlist and Radio... types
of information. A form contains form elements checkbox,
text field, radio...;/form>
Text Fields:
Text fields are used to allow
Text Field Midlet Example
Text Field MIDlet Example
This example illustrates how to insert
text field in your form... the form with name and company
field. The
TextField
SWING
SWING A JAVA CODE OF MOVING TRAIN IN
SWING