File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to save GUI in XML file and vice versa?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to save GUI in XML file and vice versa??" Watch "How to save GUI in XML file and vice versa??" New topic
Author

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

Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4638
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
i think it is different issues by different persons.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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


GenRocket - Experts at Building Test Data
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1768
    
    7

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!)


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4638
    
    5

Originally posted by Michelle Wang:
i think it is different issues by different persons.


Ha ha.
I rest my case.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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

Joined: Nov 23, 2008
Posts: 23
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

Joined: Apr 17, 2007
Posts: 87
wonderful!! Thanks
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, looks like Andre answered it on both sites. Well done.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Yes, I think my one is more generic than that one. He is really super star
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to save GUI in XML file and vice versa??