participate


Swing - XmlEncoder Error
This question is not answered.

<<   Back to Forum  |   Give us Feedback
4 Duke Stars available
This topic has 15 replies on 2 pages.    1 | 2 | Next »
FanJava
Posts:26
Registered: 9/22/09
XmlEncoder Error   
Nov 3, 2009 12:56 PM
 
 
I am successful in encoding and decoding Jtextboxes and Jlabels using XMLEncoder and XMLDecoder.
Now When i use JComboBoxes I get errors like....

java.lang.InstantiationException: javax.swing.AncestorNotifier
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement ClockAddSet.addPropertyChangeListener(AncestorNotifier);
Continuing ...
java.lang.InstantiationException: javax.swing.AncestorNotifier
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement ClockAddSet.addPropertyChangeListener(AncestorNotifier);


I know these exceptions are due to JComboBoxes only.
I think i can go about by removing the propertychange listener before encoding, but am not sure how to go about it.
It would be nice if anyone could help me in this.A small example may be?

I tried to handle the exceptions using try catch but was not successful.

The JcomboBox Panel which I am using is created in netbeans and I don't have any propertychange listeners implemented in them.
 
sabre150
Posts:21,248
Registered: 10/24/97
Re: XmlEncoder Error   
Nov 3, 2009 11:48 PM (reply 1 of 15)  (In reply to original post )
 
 
I can never understand why people want to serialize Swing components and using XMLEncoder/Decoder is a form of seriaization. What requirement forces this approach on you?
 
kopik
Posts:110
Registered: 11/15/08
Re: XmlEncoder Error   
Nov 4, 2009 1:53 AM (reply 2 of 15)  (In reply to original post )
 
 
hii,

1/ built the JcomboBox Panel from xml statements...
2/ add Items for JCombo into JcomboBox Panel from xml statements...

.. kopik
 
FanJava
Posts:26
Registered: 9/22/09
Re: XmlEncoder Error   
Nov 4, 2009 5:20 AM (reply 3 of 15)  (In reply to #2 )
 
 
@Sabre150
I am doing this so that it would be easy for anyone to have multiple configuration files saved into an XML format. Also if there is a simpler way to this can you guide me through that.

@kopik
Is there a possible way to catch these exceptions, or can I store not the entire frame but just some parts of it which might be enough to decode. This is because the XML encoder gives these exceptions but the decoder decodes it properly. So can i remove the propertychangelistener before encoding. If so may I knw how to do it?
 
kopik
Posts:110
Registered: 11/15/08
Re: XmlEncoder Error   
Nov 4, 2009 6:06 AM (reply 4 of 15)  (In reply to #3 )
 
 
ok,
for which from your objects (JcomboBox Panel) you implements propertychangelistener,
can you directly gets and translate correct values from other formated file as xml config,
if you disabling propertychangelistener, then can you uploads all objects with correct value,
if is propertychangelistener good method and valid for object,

---> so think that we are so long from real reason,

... kopik
 
FanJava
Posts:26
Registered: 9/22/09
Re: XmlEncoder Error   
Nov 4, 2009 6:23 AM (reply 5 of 15)  (In reply to #4 )
 
 
Ok so it seems to be a bad idea to disable propertychangelisteners.!!!!

But then I am using a JDesktopPane which has many internal frames in it and I am trying to save the internal frames into XML. what if I have many internal frames with combo boxes wouldn't it be difficult to built jcomboboxes from XML file. Here is the save and load snippet !!!

public void saveconfig() throws Exception {
    	
    	try {
    		JInternalFrame[] frames = desktop.getAllFrames();
    		File filedefault = new File("Dashboard.xml");
    		JFileChooser fc = new JFileChooser();
    		XMLFilter fXMLFilter = new XMLFilter ();
    		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    		fc.setCurrentDirectory (new File ("."));
    		fc.setFileFilter(fXMLFilter);
    	    fc.setAcceptAllFileFilterUsed(false);
    		fc.setSelectedFile(filedefault);
    		int returnVal = fc.showSaveDialog(Dashboard.this);
    		 if (returnVal == JFileChooser.APPROVE_OPTION) {
    			 File file =fc.getSelectedFile();
    			 if (file.exists ()) {
    	             int response = JOptionPane.showConfirmDialog (null,
    	               "Overwrite existing file?","Confirm Overwrite",
    	                JOptionPane.OK_CANCEL_OPTION,
    	                JOptionPane.QUESTION_MESSAGE);
    	             if (response == JOptionPane.CANCEL_OPTION){
    	            	 saveconfig();
    	             } 	
    	         }
    		
    	        writeFile(file,file.getName());
    			 FileOutputStream f = new FileOutputStream(file.getName());
    	    		BufferedOutputStream b = new BufferedOutputStream(f);
    	    		XMLEncoder e = new XMLEncoder(b);
    	    		e.writeObject(frame.getBounds());
    	    		
    	    		for(int i = 0; i < frames.length; i++) {
    	    			try{
    	    				e.writeObject(frames[i]);
    	    			}catch(Throwable t){
    	    				
    	    			}
    	    		}
    	    		
    	    		e.close();
    	    		
    		 }else if (returnVal == JFileChooser.CANCEL_OPTION){
    		 }	
    	} catch(FileNotFoundException e) {}
    	
    }
    
  
   public static boolean writeFile (File file, String dataString) {
     try {
        PrintWriter out =
          new PrintWriter (new BufferedWriter (new FileWriter (file)));
        out.print (dataString);
        out.flush ();
        out.close ();
     }
     catch (IOException e) {
        return false;
     }
     return true;
   } // writeFile
    
    public void loadconfig() throws Exception {
    	try {
    		
    	    JFileChooser fc = new JFileChooser();
    	    XMLFilter fXMLFilter = new XMLFilter ();
    	    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    	    fc.setCurrentDirectory (new File ("."));
    	    fc.setFileFilter(fXMLFilter);
    	    fc.setAcceptAllFileFilterUsed(false);
    	    int returnVal = fc.showOpenDialog (Dashboard.this);
    	    if (returnVal == JFileChooser.APPROVE_OPTION) {
    	    	File file = fc.getSelectedFile ();
    	    	FileInputStream  f = new FileInputStream(file.getName());
    	    	BufferedInputStream b = new BufferedInputStream(f);
    	    	XMLDecoder d = new XMLDecoder(b);
    	    	Vector<Object> v = new Vector<Object>();
    	    	boolean next = true;
    	    	desktop.removeAll();
    	    	while (next) {
    	    		try {
    	    			v.add(d.readObject());
    	    		} catch(ArrayIndexOutOfBoundsException e) {
    	    			d.close();
    	    			next = false;
    	    		}
    	    	}
    	    	Object[] frames = v.toArray();
    	    	
    	    	frame.setBounds((Rectangle) frames[0]);
    	    	for(int i = 1; i < frames.length; i++) {
    	    			
    	    			JInternalFrame fr = (JInternalFrame) frames[i];
    	    			fr.setBounds(frame.getNormalBounds());
    	    			desktop.add(fr);
    	    			fr.show();
    	    	
    	    	}
    	    }else if(returnVal == JFileChooser.CANCEL_OPTION){
    	    }
    	}catch(FileNotFoundException e) {
    		int response = JOptionPane.showConfirmDialog (null,
 	               "Invalid File Name","File Not Found",
 	                JOptionPane.OK_CANCEL_OPTION,
 	                JOptionPane.QUESTION_MESSAGE);
    		if (response == JOptionPane.CANCEL_OPTION){
            }else if (response == JOptionPane.OK_OPTION){
            	loadconfig();
            }
    	}       
    }
    
 
FanJava
Posts:26
Registered: 9/22/09
Re: XmlEncoder Error   
Nov 4, 2009 6:29 AM (reply 6 of 15)  (In reply to #5 )
 
 
My questions is that do I need to save the whole frame object or can I go about by saving just some components of it in order to load it appropriately??
 
FanJava
Posts:26
Registered: 9/22/09
Re: XmlEncoder Error   
Nov 4, 2009 7:04 AM (reply 7 of 15)  (In reply to #6 )
 
 
can Anyone help me on this please!!!
 
WalterLaan
Posts:816
Registered: 4/17/08
Re: XmlEncoder Error   
Nov 4, 2009 7:45 AM (reply 8 of 15)  (In reply to #7 )
 
 
I just tested and JComboBox (de)serializes ok (including AnchestorListeners), so you have to make an minimal working example which shows the problem.
 
FanJava
Posts:26
Registered: 9/22/09
Re: XmlEncoder Error   
Nov 4, 2009 7:54 AM (reply 9 of 15)  (In reply to #8 )
 
 
Yes it deserializes perfectly fine but during the time of saving i get exceptions. How to catch those exceptions or get rid of them???

@WalterLaan: When u tried to save the combobox did you not get exceptions like the ones which i posted above???(1st thread)
 
kopik
Posts:110
Registered: 11/15/08
Re: XmlEncoder Error   
Nov 4, 2009 8:03 AM (reply 10 of 15)  (In reply to #6 )
 
 
ok,

save only components name with values, starts with <header primare = "yourIntFrameName">additional values ...</header>

snapshot from object, where are components and its values wraped up under(with) <compName> values from comp.. </compName>

1/ in case that intFrames had strict number of components, then why implements strict object twice or clone it to the output,
a/ have to set (virtual) ordering and "only" save the values valid for component,
b/ for this actions is xml struct (saved to db) suitable way,
c/ anyway is App terminated, then repeated "Open App" loads last know values,
d/ including size, bounds, overlay on container, if you want to save, and built intFrame by checking this value,
e/ how you want to save jCombo, its JComboList and selectedItems from JComboList (valid for table, ...),
f/ set the unique name for objects (isn't strict or necessary),
g/ then you can use only template(s),

2/ if not, then you must to set the unique name for each from your components,
a/ getComponent() to array, list or String,
b/ add getComponentName(),
c/ then your xml must implement these conditions, unique name, object type (JTextField, JComboBox, ...), type, size, bounds, overlay on container, then values,

3/ it will be hard work if you to calculate with only one xml template for whole app, I saved this values to multiDimensional object (similiar or equals xml logics) prepared for another actions (new comp = new row, new row = new properties), real output sometime = null, but is built and periodicaly updated,

4/ sry, I'll check yr code tomorow (if anybody response)

5/ maybe save thats from prepared template can save your development time (yes, then you have to write some things twice)

... kopik
 
FanJava
Posts:26
Registered: 9/22/09
Re: XmlEncoder Error   
Nov 4, 2009 8:07 AM (reply 11 of 15)  (In reply to #9 )
 
 
Also I have used netbeans to create JcomboBoxes!!!
 
FanJava
Posts:26
Registered: 9/22/09
Re: XmlEncoder Error   
Nov 4, 2009 10:18 AM (reply 12 of 15)  (In reply to #10 )
 
 
@kopik : Thanks for the instructions will try to implement that but I guess there should be a workaround for this, as this involves duplicating my code a places. Hope you can have a glance at my code and u might find something.


@WalterLaan: is there a way to get rid of my exceptions?? Also when u serialized it did u find any errors?? Did you use my code in that process??
 
jduprez
Posts:1,169
Registered: 10/2/98
Re: XmlEncoder Error   
Nov 4, 2009 2:00 PM (reply 13 of 15)  (In reply to #3 )
 
 
I assume what happens is that the type javax.swing.AncestorNotifier is not a bean (no no-arg constructor).

I can never understand why people want to serialize Swing components and using XMLEncoder/Decoder is a form of seriaization. What requirement forces this approach on you?
FanJava wrote:
@Sabre150
I am doing this so that it would be easy for anyone to have multiple configuration files saved into an XML format. Also if there is a simpler way to this can you guide me through that.

I don't think sabre asked why you serialize the frames in XML, but why you serialize the frames.
If you want to save the display configuration to restore it when relaunching the app, you don't need to serialize the whole frames, but merely, for each frame:
- its size
- its position
- its content (in most cases, an id of some model entity specific to your application, along with a few contextual information, like scrolling position,...)

Of course you'd need application-specific code to instantiate and populate the internal frames according to the deserialized sizes, positions, contentIds and contexts). That may appear cumbersome, but even if you could serialize and deserialize your frames, most probably you also need such app-specific code to properly wire the frames to the model layer - assuming the app is designed this way.
 
kopik
Posts:110
Registered: 11/15/08
Re: XmlEncoder Error      
Nov 5, 2009 12:58 AM (reply 14 of 15)  (In reply to #13 )
 
 
hii,

FanJava sent:

                if (returnVal == JFileChooser.APPROVE_OPTION) {
    	    	File file = fc.getSelectedFile ();
    	    	FileInputStream  f = new FileInputStream(file.getName());
    	    	BufferedInputStream b = new BufferedInputStream(f);
    	    	XMLDecoder d = new XMLDecoder(b);
    	    	Vector<Object> v = new Vector<Object>();
    	    	boolean next = true;
    	    	desktop.removeAll();
    	    	while (next) {
    	    		try {
    	    			v.add(d.readObject());
    	    		} catch(ArrayIndexOutOfBoundsException e) {
    	    			d.close();
    	    			next = false;
    	    		}
    	    	}
    	    	Object[] frames = v.toArray();
    	    	
    	    	frame.setBounds((Rectangle) frames[0]);
    	    	for(int i = 1; i < frames.length; i++) {
    	    			
    	    			JInternalFrame fr = (JInternalFrame) frames[i];
    	    			fr.setBounds(frame.getNormalBounds());
    	    			desktop.add(fr);
    	    			fr.show();
    	    	
    	    	}
    	    }else if(returnVal == JFileChooser.CANCEL_OPTION){
    	    }


pls only notes:

1/ desktop.removeAll(); call to the g.c., then you finally kill any listener, settings, prepared value, any object linked, validations to the object,
2/ desktop.removeAll(); in case that I should be sure what's happeneds, that I'll be able to set all methods, can implements, built object, ... again,
3/ MDI (containers for internal frame) can implements lists of valid objects, then you can call intFrame = null, instead of desktop.removeAll();,
4/ u can use yr code for developt issues,

... kopik
 
This topic has 15 replies on 2 pages.    1 | 2 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums