Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36

Thread: Can't get it to save stuff with JFileChooser

  1. #26
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Quote Originally Posted by Norm View Post
    Please explain "what I want".


    No you can use any file extension that you want. Chose one that won't confuse the OS. Like .dat or .ser

    Best way to answer all your questions is to write a small simple program with a small simple class with some data and a get method. Create an object from the small simple class with some data, serialize it to a file, read the object back in and call the method to get the saved data to see it matches what was saved originally.
    So do you use writeObject() to serialize it?

    If not, how do you serialize it?

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    do you use writeObject()
    Yes

  3. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (August 10th, 2010)

  4. #28
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    java.io.NotSerializableException: java.io.ObjectOutputStream
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unkn own Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unk nown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at javax.swing.event.EventListenerList.writeObject(Un known Source)
    at sun.reflect.GeneratedMethodAccessor10.invoke(Unkno wn Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknow n Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unk nown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unkn own Source)
    at java.io.ObjectOutputStream.defaultWriteObject(Unkn own Source)
    at javax.swing.JComponent.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknow n Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unk nown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.access$300(Unknown Source)
    at java.io.ObjectOutputStream$PutFieldImpl.writeField s(Unknown Source)
    at java.io.ObjectOutputStream.writeFields(Unknown Source)
    at java.awt.Container.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknow n Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unk nown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unkn own Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unk nown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unkn own Source)
    at java.io.ObjectOutputStream.defaultWriteObject(Unkn own Source)
    at javax.swing.JMenu.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknow n Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unk nown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.access$300(Unknown Source)
    at java.io.ObjectOutputStream$PutFieldImpl.writeField s(Unknown Source)
    at java.io.ObjectOutputStream.writeFields(Unknown Source)
    at java.awt.Container.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknow n Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unk nown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.access$300(Unknown Source)
    at java.io.ObjectOutputStream$PutFieldImpl.writeField s(Unknown Source)
    at java.io.ObjectOutputStream.writeFields(Unknown Source)
    at java.awt.Container.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknow n Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unk nown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at SongBook.LivingHymns$46.actionPerformed(LivingHymn s.java:2042)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unk nown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.plaf.basic.BasicMenuItemUI.getMenuItem Parent(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.layoutMenuI tem(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.getPreferre dMenuItemSize(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.getPreferre dSize(Unknown Source)
    at javax.swing.JComponent.getPreferredSize(Unknown Source)
    at javax.swing.BoxLayout.checkRequests(Unknown Source)
    at javax.swing.BoxLayout.minimumLayoutSize(Unknown Source)
    at java.awt.Container.minimumSize(Unknown Source)
    at java.awt.Container.getMinimumSize(Unknown Source)
    at javax.swing.JComponent.getMinimumSize(Unknown Source)
    at javax.swing.BoxLayout.checkRequests(Unknown Source)
    at javax.swing.BoxLayout.preferredLayoutSize(Unknown Source)
    at java.awt.Container.preferredSize(Unknown Source)
    at java.awt.Container.getPreferredSize(Unknown Source)
    at javax.swing.JComponent.getPreferredSize(Unknown Source)
    at javax.swing.ScrollPaneLayout.layoutContainer(Unkno wn Source)
    at java.awt.Container.layout(Unknown Source)
    at java.awt.Container.doLayout(Unknown Source)
    at java.awt.Container.validateTree(Unknown Source)
    at java.awt.Container.validate(Unknown Source)
    at javax.swing.RepaintManager.validateInvalidComponen ts(Unknown Source)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  5. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Go read post#12

    Did you understand what the API doc said for that Exception?

  6. #30
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    How do you get an object to implements the Serializable interface?

    I have the entire class implementing Serializable.

    Anyway, Serializable has no methods.

    Also, so far it appears all of my components on the panel are Serializable.

    How did I get that exception thrown then?

  7. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    How do you get an object to implements the Serializable interface?
    To have a class implement an interface:

    class ClassName implements Serializable {

    How did I get that exception thrown then?
    Add this just before the writeObject(theObject):
    System.out.println("obj class=" + theObject.getClass()); // show objects class before writing it

    Buried in the middle of that long stack trace:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException ????<<<<<????

    have you written a small test program as I suggested in post#25.
    Did you have any problems with it? If so, post the error messages and the source for the small program.
    DO NOT POST YOUR HUGH program. Only a small one.

  8. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (August 10th, 2010)

  9. #32
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: Can't get it to save stuff with JFileChooser

    import java.awt.color.ColorSpace;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
     
    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;
    import javax.swing.event.PopupMenuEvent;
    import javax.swing.event.PopupMenuListener;
    import javax.swing.filechooser.FileSystemView;
    import javax.swing.text.DefaultStyledDocument;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import java.util.*;
    import java.util.jar.JarFile;
    import java.io.*;
     
    import javax.swing.ImageIcon;
     
     
    import java.awt.event.WindowEvent;
    import java.awt.print.PageFormat;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import java.awt.Window;
    import javax.swing.JScrollPane;
    import java.awt.Component;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    // import acm.io.*;
    //import acm.graphics.*;
    //import acm.program.*;
    //import acm.gui.*;
    //import acm.util.*;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Paint;
    import java.awt.PrintJob;
    import java.awt.Transparency;
    import java.awt.print.*;
     
     
    public class loadNSave extends JFrame implements Serializable
    {
    JButton button, button2, button3, button4, button5;
    JTextArea area;
    JMenuBar bar;
    JMenu menu;
    JMenuItem save, load;
    JPanel panel;
    JScrollPane pane, pane2;
    JFrame frame, saveFrame, loadFrame;
    File file, file2;
    ObjectInputStream inputStr;
    ObjectOutputStream outputStr;
    FileInputStream pond;
    FileOutputStream lake;
    JFileChooser chooser, chooser2;
     
    public loadNSave() 
    {
    setTitle("Title");
    setVisible(true);
    saveFrame = new JFrame();
    loadFrame = new JFrame();
    button = new JButton("Button");
    panel = new JPanel();
    pane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    frame = new JFrame();
    button.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.INFORMATION_MESSAGE);
    }} );
    button2 = new JButton("Button");
    button2.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.PLAIN_MESSAGE);
    }});
     
    button3 = new JButton("Button");
    button3.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.QUESTION_MESSAGE);
    }} );
     
    button4 = new JButton("Button");
    button4.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.WARNING_MESSAGE);
    }});
     
    button5 = new JButton("Button");
    button5.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(frame, "Message", "Another Message", JOptionPane.ERROR_MESSAGE);
    }});
     
    area = new JTextArea(100, 100);
    pane2 = new JScrollPane(area, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     
    bar = new JMenuBar();
    menu = new JMenu("File");
    bar.add(menu);
    save = new JMenuItem("Save");
    load = new JMenuItem("Load");
    menu.add(save);
    menu.addSeparator();
    menu.add(load);
    save.addActionListener(
    					new ActionListener() {
     
    						@Override
    						public void actionPerformed(ActionEvent arg0) {
    							// TODO Auto-generated method stub
    						//	saveDialog.setVisible(true);
     
     
     
    							chooser = new JFileChooser();
    							chooser.setApproveButtonToolTipText("Click here to save File");
     
     
    							chooser.setBackground(Color.GREEN);
    							chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
     
    							 chooser.setVisible(true);
    							chooser.showSaveDialog(saveFrame);
    							System.out.println(chooser.getSelectedFile());
    							file = chooser.getSelectedFile();
    							String bob3 = chooser.getSelectedFile().getName();
     
    							System.out.println(file.getFreeSpace());
    							file.setReadable(true);
    							try {
    								lake = new FileOutputStream(file);
    							} catch (FileNotFoundException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
    							try {
    								outputStr = new ObjectOutputStream(lake);
    							} catch (IOException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
     
     
    							try {
    								outputStr.writeObject(panel);
    							} catch (IOException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
     
     
     
    							try {
    								outputStr.writeObject(pane2);
    							} catch (IOException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
     
    							try {
    								outputStr.writeObject(button);
    							} catch (IOException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}
     
     
     
    						}
     
    					});
     
    load.addActionListener(
    				 new ActionListener() {
    					 public void actionPerformed(ActionEvent e)
    					 {
    						 chooser2 = new JFileChooser();
    						 chooser2.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
     
    						 chooser2.setVisible(true);
    						chooser2.showOpenDialog(loadFrame);
     
    						System.out.println(chooser2.getSelectedFile());
    						file2 = chooser2.getSelectedFile();
     
     
    						String bob = file2.getName() + " doesn't exist!";
    						String bob2 = file2.getName() + " does exist.";
     
    						if (file2.exists() == true)
    						{
    							JOptionPane.showMessageDialog(null, "File exists", bob2, JOptionPane.INFORMATION_MESSAGE);
    						}
     
    						else if (file2.exists() == false)
    						{
    							JOptionPane.showMessageDialog(null, "File not found", bob, JOptionPane.ERROR_MESSAGE);
    						}
     
    						try {
    							pond = new FileInputStream(file2);
    						} catch (FileNotFoundException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    						try {
    							inputStr = new ObjectInputStream(pond);
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    						try {
    							pond.read();
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    						try {
    							pond.getFD();
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    						try {
    							System.out.println(pond.getFD());
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
     
    						try {
    							inputStr.readObject();
    						} catch (IOException e2) {
    							// TODO Auto-generated catch block
    							e2.printStackTrace();
    						} catch (ClassNotFoundException e2) {
    							// TODO Auto-generated catch block
    							e2.printStackTrace();
    						}
     
    						System.out.println(pond.getChannel());
     
    						System.out.println(pond.getChannel().isOpen());
     
    						System.out.println(file2.canRead());
    						System.out.println(file2.canExecute());
    						System.out.println(file2.getTotalSpace());
    						file2.setExecutable(true);
     
    					 }
    				 });
    panel.add(button);
    panel.setVisible(true);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    panel.add(button5);
    panel.add(pane2);
    panel.add(bar);
    bar.setVisible(true);
     
     
     
     
     
    getContentPane().add(pane);
    }
     
    public static void main(String[]args)
    {
    loadNSave ref = new loadNSave();
    }
     
     
    }

    The problems are that it won't close even after I click the x.

    Also, it doesn't appear to save or load correctly, as it's not bringing up the new stuff.

    Also, no error messages are being generated.

    I''m only getting error messages with huge program.

    However, small program doesn't appear to be able to load and save properly either.

    Do I need to get it to remove all the components first before reading in the objects?

    Also, when I serialize the JPanel, does it have to serialize the ObjectINputStream itself, because that class isn't serializable.


  10. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    A 300 line file is not a small program.
    You don't need any GUI to test Serialization with. Just some classes to write and read back.
    The frame doesn't show any components and doesn't handle window closing.

    Try again. This test program is too hand to work with.

  11. #34
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    Wait, can I get it to write an entire program, like have a small program, then have, in another file in the Eclipse project, write the first class into the ObjectOutputStream?

    Is that what you meant?

    I'm afraid I don't quite understand what you're saying.

    I don't think my big file can serialize the graphics the paint method put in; that may be part of the problem.

    How do I deal with that?

    How do you read and write the not Serializable stuff?

    I was going to make a bit of code that would check each item and use a try and catch block to check if it's serializable, and if it's not, to make it read and write anyway as I read there's a way to do it, but I have to do something with all of its fields and stuff like that.

    Do you mean by fields all the stuff under Fields in the javaDoc online?

    Also, to load, do I have to clear the current components visible first before it'll work?

  12. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    You don't serialize the java program. You only need to save the current values in variables like ints and Strings.

    For your test program, remove ALL of the GUI. No frames, panels, buttons etc.

    Read the Java Tutorial about Serialization:
    Object Streams

    Just as data streams support I/O of primitive data types, object streams support I/O of objects. Most, but not all, standard classes support serialization of their objects. Those that do implement the marker interface Serializable.

    The object stream classes are ObjectInputStream and ObjectOutputStream. These classes implement ObjectInput and ObjectOutput, which are subinterfaces of DataInput and DataOutput. That means that all the primitive data I/O methods covered in Data Streams are also implemented in object streams. So an object stream can contain a mixture of primitive and object values. The ObjectStreams example illustrates this. ObjectStreams creates the same application as DataStreams, with a couple of changes. First, prices are now BigDecimalobjects, to better represent fractional values. Second, a Calendar object is written to the data file, indicating an invoice date.

    If readObject() doesn't return the object type expected, attempting to cast it to the correct type may throw a ClassNotFoundException. In this simple example, that can't happen, so we don't try to catch the exception. Instead, we notify the compiler that we're aware of the issue by adding ClassNotFoundException to the main method's throws clause.

    Output and Input of Complex Objects
    The writeObject and readObject methods are simple to use, but they contain some very sophisticated object management logic. This isn't important for a class like Calendar, which just encapsulates primitive values. But many objects contain references to other objects. If readObject is to reconstitute an object from a stream, it has to be able to reconstitute all of the objects the original object referred to. These additional objects might have their own references, and so on. In this situation, writeObject traverses the entire web of object references and writes all objects in that web onto the stream. Thus a single invocation of writeObject can cause a large number of objects to be written to the stream.

    This is demonstrated in the following figure, where writeObject is invoked to write a single object named a. This object contains references to objects b and c, while b contains references to d and e. Invoking writeobject(a) writes not just a, but all the objects necessary to reconstitute a, so the other four objects in this web are written also. When a is read back by readObject, the other four objects are read back as well, and all the original object references are preserved.

    I/O of multiple referred-to objects

    I/O of multiple referred-to objects
    You might wonder what happens if two objects on the same stream both contain references to a single object. Will they both refer to a single object when they're read back? The answer is "yes." A stream can only contain one copy of an object, though it can contain any number of references to it. Thus if you explicitly write an object to a stream twice, you're really writing only the reference twice. For example, if the following code writes an object ob twice to a stream:

    Object ob = new Object();
    out.writeObject(ob);
    out.writeObject(ob);

    Each writeObject has to be matched by a readObject, so the code that reads the stream back will look something like this:

    Object ob1 = in.readObject();
    Object ob2 = in.readObject();

    This results in two variables, ob1 and ob2, that are references to a single object.

    However, if a single object is written to two different streams, it is effectively duplicated — a single program reading both streams back will see two distinct objects.
    Last edited by Norm; August 10th, 2010 at 05:38 PM.

  13. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (August 10th, 2010)

  14. #36
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Can't get it to save stuff with JFileChooser

    I haven't read the entire thread, but it looks to me like you can do whatever it is you're doing with java.util.prefs.Preferences

    db

  15. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    javapenguin (August 13th, 2010)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Using JFileChooser to open and display an image
    By JavaN0ob in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 31st, 2010, 06:01 PM
  2. Noob, Helloworld stuff not working? Help?
    By Bdavis93 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 7th, 2010, 08:29 PM
  3. JFileChooser
    By FretDancer69 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 3rd, 2010, 06:35 AM
  4. VB can do stuff Java can`t ??
    By JFujy in forum Java Theory & Questions
    Replies: 2
    Last Post: September 22nd, 2009, 10:25 AM
  5. Replies: 2
    Last Post: March 6th, 2009, 03:00 PM