Click here to Skip to main content
12,945,706 members (49,941 online)
Rate this:
 
Please Sign up or sign in to vote.
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:
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 18-Oct-10 1:35am
faseem321
Updated 18-Oct-10 1:43am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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");
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,170
Maciej Los 2,443
Jochen Arndt 1,910
ppolymorphe 1,775


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 18 Oct 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100