• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to save GUI in XML file and vice versa??

 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sir:

I google a good example that meets my project requirements as code below,
I need to save it in XML format in disk with what I change/draw on the GUI, ie, I draw 10 lines(5 vertical and 5 horizontal) on it, then save it to MySaveLine.xml
then quit, later, I open project, it read this MySaveLine.xml from disk, and it automatically display all what I saved before ie:10 lines(5 vertical and 5 horizontal), it should be exactly same as what I did before.

But I have no idea how to do it,
Can Guru help here??

Thanks



 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michelle Wang:
But I have no idea how to do it,
Can Guru help here??

I don't know, why don't you ask him?
(Sorry, couldn't hold back )


You only need to save and load the lines right? As in the contents of "List lines"?

If so it shouldn't be that hard.

Step 1: create your XML layout.
I'd go for the following:

Because that's what a line is: a start point and an end point, and each point has an X and Y coordinate.

Step 2: saving to XML. This is quite easy. Get a FileWriter, wrap it in a PrintWriter, and write everything:


Step 3: reading from XML. I'd use a library like JDOM. You let it parse the XML, and you read the elements. In pseudo code:

I'm sure you can fill in the blanks.


But if it were up to me, I'd serialize and deserialize the entire List of lines. It would be a lot easier, and you prevent someone opening your XML file with a text editor and messing it up.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posting different code under different usernames doesn't make this any less of a cross post.
http://forums.sun.com/thread.jspa?threadID=5349871

7 minutes apart.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think it is different issues by different persons.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darryl Burke:
Posting different code under different usernames doesn't make this any less of a cross post.
http://forums.sun.com/thread.jspa?threadID=5349871

7 minutes apart.


It's interestingly close. Especially the usernames on both sites. Sunnymanman and Sunnygirl. Regardless, there is nothing wrong with posting the same questions on more than one site. We just don't like it in more than one forum. We just want you to be honest about it.

http://faq.javaranch.com/java/BeForthrightWhenCrossPostingToOtherSites
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:

Because that's what a line is: a start point and an end point, and each point has an X and Y coordinate.


Not to be a smart-ass, but that's actually not what a line is
A line segment is bound by two end points, a line streches out to infinity (and beyond captain!)
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michelle Wang:
i think it is different issues by different persons.


Ha ha.
I rest my case.
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jelle Klap:


Not to be a smart-ass, but that's actually not what a line is
A line segment is bound by two end points, a line streches out to infinity (and beyond captain!)

A mathematical line yes; in Java it does have two points defining the line. Probably because "This Line2D represents a line segment in (x,y) coordinate space."
 
Andre Uhres
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Michelle Wang,
well done for showing a working example right away!
Here is one way to meet your project requirements using XMLEncoder:

1. Add these new instance variables before "List lines = new ArrayList ();":


2. Add these imports:


3. Add these two methods:


4. Create this new class file:


5. Change all "Line2D.Double" to "MyLine" in your "LineDemo" file.

6. Add a call to "save();" in the "paintComponent" method.

7. Add this initializer after "List lines = new ArrayList ();":
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wonderful!! Thanks
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, looks like Andre answered it on both sites. Well done.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think my one is more generic than that one. He is really super star
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic