Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Long Term Persistence, XMLEncoder  RSS feed

 
Ladislau Bogdan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all!
Sorry, maybe this is not the right thread where I have to post this question!
I'm using java.beans.XMLEncoder and java.beans.Decoder to save the content of a frame (JFrame)in a XML file, and after this I want to rebuild the frame. My problem is that I have some JButtons with icons, and XML part for a button looks like:
<object class="javax.swing.JButton">
<string>jButton1</string>
<void property="enabled">
<boolean>false</boolean>
</void>
<void property="icon">
<object class="javax.swing.ImageIcon">
<string>file:/C:/temp/project_name/resource/button/Close.gif</string>
</object>
</void>
</object>
So I have the path to the image file here. The problem is that when I'm trying to display the frame, rebuilding with XMLDecoder, the icon doesn't appear. If I'm chaging: "file:/C:/temp/project_name/resource/button/Close.gif" to "C:/temp/project_name/resource/button/Close.gif" in the XML file, then everything is ok.
Does anybody know why doesn't work before I change this line?
Thx in advance!
:roll:
P.S.: If I have the images in a jar file that line looks like: "jar:file:/D:/project_name/project_name.jar!/project_name/resource/button/Close.gif
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you load the gif from the file system you have to provide a valid file path, IMHO?! if you cut the "file:" part away, would that be a solution?
what concerns the jar file: you need the class loader and the relative path inside the jar file to load the gif. in you case the code would look like:

I haven't tried to use the path with the "!" though. Maybe that might work in some other way, as well.
chantal
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to add: you don't need to specify the exact jar file. the class loader will look in any file in the classpath.
 
Ladislau Bogdan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code:
package convertorxml;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.*;
public class Frame2 extends JFrame {
private JPanel jPanel1 = new JPanel();
private JButton jButton1 = new JButton();
private FlowLayout flowLayout1 = new FlowLayout();
private FlowLayout flowLayout2 = new FlowLayout();
public Frame2() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(flowLayout1);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setLayout(flowLayout2);
ImageIcon icn = new ImageIcon(new java.net.URL("file:///C:/temp/arnold/resource/button/Close.gif"));
jButton1.setIcon(icn);
jButton1.setText("jButton1");
this.getContentPane().add(jPanel1, null);
jPanel1.add(jButton1, null);
}
}
Where "file:///C:/temp/project_name/resource/button/Close.gif" it's a valid path, and here is the code to generate the xml file and rebuid the frame:

JFrame rootWindow;
rootWindow = new Frame2();
try{
XMLEncoder enc = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("c:\\temp\\Test.xml")));
JFrame frameul = new JFrame();
frameul.setContentPane(rootWindow.getContentPane());
frameul.setTitle(rootWindow.getTitle());
enc.writeObject(frameul);
Object server = new Object();
enc.setOwner(server);
enc.setExceptionListener(new ExceptionListener() {
public void exceptionThrown (Exception e) { }
});
enc.writeStatement(new Statement(server, "show", new Object[]{ frameul, frameul}));
enc.close();
}catch(FileNotFoundException aa)
{
aa.printStackTrace();
}
and:
try{
XMLDecoder d = new XMLDecoder(
new BufferedInputStream(
new FileInputStream("c:\\temp\\Test.xml")));
Object result = d.readObject();
d.close();
JFrame frame = (JFrame) result;
frame.show();
}catch(FileNotFoundException ab)
{
ab.printStackTrace();
}
If you will try it, take a look in c:\temp\Test.xml
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!