• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Save button within JFrame not working

 
John Paul Hoey
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Firstly, apologies if I have posted this in the wrong section however i think my problem is more relevant to Swing rather than XML.

I have a programme that when a button is selected on the main JFrame, an XML file is loaded into memory and a document / object created; this works fine.

I then pass this XML document / object as a parameter to a constructor onto another JFrame to be used within this second JFrame; again this works fine.

However, my problem lies when i try and save this object by using a save button i have created on the second JFrame; when i press the save button, i get a blank XML document i.e. the initial document hasnt saved.


Here is the code for the first button on the first JFrame which loads the XML file and creates an object:



Here is the Constructor on the second JFrame that gets the XML object / document from the first JFrame:




And here is the code within the Save button which i want to take the XML document / object above and save it out to a file:




As i previously said, when i save the file, it creates a blank XML file which is obviously wrong.

I know for a fact that the object is being passed to the constructor because when i copied the save button code within the constructor, the expected file was created successfully when i pressed the load button on the first JFrame, therefore my problem seems that the XML document / object doesnt appear to be getting passed to the save button.

My understanding (which could be wrong as im a java novice) was that the public (global) variable that i have created at the top of the second JFrame (i.e. public Document doc = null) would get set to the doc value within the constructor however it appears my understanding of how this works is wrong.

Has anyone got any suggestions on how i can get this to work and how to pass the value to the save button on the second JFrame?

Thanks in advance

John

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> My understanding (which could be wrong as im a java novice) was that the public (global) variable that i have created
>at the top of the second JFrame (i.e. public Document doc = null) would get set to the doc value within the constructor
> however it appears my understanding of how this works is wrong.

your understanding is OK, your execution is wrong.

 
John Paul Hoey
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

Thanks for the response - really appreciated.

The issue I had in my code which you highlighted makes sense i.e. i was actually creating 2 seperate instances of CMSPage1 rather than just the one.

To get around this problem, I changed the setVisible method line as follows:



By doing this, i assumed that instead of creating 2 seperate instances of CMSPage1, i would only create the 1 and this new page would then recieve the doc object via the parameter ... however this doesnt seem to be the case.

When i run my code now, the new page comes up blank, whereas before, it contained all my textfields and additional buttons etc.

However, what i have noticed is that if i remove the doc parameter when creating the object (i.e. CMSPage1 nf = new CMSPage1();), the page loads successfully.

Am i doing something wrong?

Any further help would be greatly appreciated

Thanks in advance
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading this on a mobile, so the code
is difficult to read, but you might want to
try adding initComponents() to the constructor
that takes the 'doc' argument
 
John Paul Hoey
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

Thanks for your help again.

As suggested, i moved the initComponents() to the constructor that was recieving the 'doc' argument and my components did appear on the new form - so thanks very much for this.

Also, my save button is now working successfully - which i couldnt have done without your help Michael.

Thanks again for all your suggestions

Regards

John
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic