Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made a JFrame parent and Jdialog child. But I am not able to access the modified value of the variable
of parent from the child class. code:
Java
import [swing packages needed];

public class grading extends JFrame implements ActionListener
{
	public int me=4; //this one
	JTextField jt;

grading()
{
	Container c=getContentPane();
	// set the layout

	jt=new JTextField(30);
	jt.addActionListener(this);
	c.add(jt);

	setSize(800,600);
	show();

}

public void actionPerformed(ActionEvent a) // pressed enter key in the textfield
{
	me=2; //changed me value

	JOptionPane.showMessageDialog(null,(me)+" is me from parent"); //displayed : 2

	child jd=new child(this); //calls dialog
	jd.setVisible(true);
	}

public static void main(String ar[])
{
	grading Gr=new grading();
	//close window handling 
}

}


class child extends JDialog
{
	JTextField jta[];

child(JFrame frame)
{
	super(frame,true);
	Container c=getContentPane();
	c.setLayout(new FlowLayout());

	grading gr=new grading();
	JOptionPane.showMessageDialog(null,(gr.me)+" is me from child"); //shows 4

	setSize(400,300);
	setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}

Recommended modifications?
Posted
Updated 18-Oct-10 1:43am
v2

1 solution

You're creating a second grading in the child class, rather than accessing the parent class.

Try replacing the lines:
grading gr=new grading();
JOptionPane.showMessageDialog(null,(gr.me)+" is me from child"); //shows 4


with
JOptionPane.showMessageDialog(null,(frame.me)+" is me from child");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900