How to Use the xmlencoder and xmldecoder classes to save window objects in swing

Source: Internet
Author: User

The xmlencoder class is a complementary replacement of objectoutputstream and can be used to generate the text representation of the JavaBean. It is used in the same way as the binary representation used to create a serializable object using objectoutputstream.

The xmldecoder class is used to read XML documents created using xmlencoder. Its purpose is similar to objectinputstream. For example, you can use the followingCodeSegment to read the first object defined in XML format (written through the xmlencoder class:

In this example, create a jframe, use xmlencoder to save the jframe object to the XML file, read the saved XML file through the xmldecoder object, and display the jframe.

Import Java. AWT. borderlayout; import Java. AWT. dimension; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. beans. xmldecoder; import Java. beans. xmlencoder; import Java. io. bufferedinputstream; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import javax. swing. abstractlistmodel; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jlist; import javax. swing. jscrollpane; public class jbenchframe extends jframe {borderlayout borderlayout1 = new borderlayout (); jlist list1 = new jlist (); jbutton fillbutton = new jbutton (); public static void main (string [] ARGs) throws filenotfoundexception {// jbenchframe Bm = new jbenchframe (); /// xmlencoder E = new xmlencoder (// new bufferedoutputstream (// new fileoutputstream ("test. XML "); // E. writeobject (BM); // E. close (); xmldecoder d = new xmldecoder (New bufferedinputstream (New fileinputstream ("test. XML "); jframe result = (jframe) d. readobject (); D. close (); result. setvisible (true);} public jbenchframe () {This. getcontentpane (). setlayout (borderlayout1); this. setsize (new dimension (400,300); fillbutton. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {listdata LD = new listdata (); long tmstart = system. currenttimemillis (); list1.setmodel (LD); list1.repaint (); long tmend = system. currenttimemillis (); system. out. println (tmend-tmstart) ;}}); fillbutton. settext ("fill"); this. getcontentpane (). add (New jscrollpane (list1), borderlayout. center); this. getcontentpane (). add (fillbutton, borderlayout. south); setdefaclocloseoperation (jframe. exit_on_close) ;}} class listdata extends abstractlistmodel {string [] strings = new string [10000]; Public listdata () {for (INT I = 0; I <10000; I ++) {strings [I] = "Bob" + I ;}} public int getsize () {return strings. length;} public object getelementat (INT index) {return strings [Index] ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.