Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: JavaSwingdialog
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
Edited 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
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 490
1 Sergey Alexandrovich Kryukov 316
2 Shai Vashdi 264
3 OriginalGriff 250
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,315
2 Peter Leow 4,040
3 Maciej Los 3,535
4 Abhinav S 3,308


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 18 Oct 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid