Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Swing dialog
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
0 OriginalGriff 365
1 Prakriti Goyal 177
2 Sergey Alexandrovich Kryukov 155
3 jlopez788 144
4 _Amy 110
0 OriginalGriff 6,837
1 Sergey Alexandrovich Kryukov 5,559
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 18 Oct 2010
Copyright © CodeProject, 1999-2014
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