Results 1 to 5 of 5

Thread: Serialization

  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Serialization

    This is actually a two part question. I understand serialization and I will have very little trouble implementing it. However, I am unsure how to write only changed objects. For instance, save as... would open the save chooser and let you choose where to save, and when you click the regular save it should write new items to the same file, but I am unsure how to do this.

    I thought of perhaps deleting the file than re-serializing it, but this seems like it could be dangerous. What is the expected way to do this?

    I have another question which isn't necessarily as specific, it relates to what is a more favored(I guess thats the word), way of creating gui's.

    I have read and seen a couple of approaches and I don't want to be getting into a bad habit. Is it better to inherit from JFrame

    Java Code:
    class MyClass extends JFrame{
    And then do all the setup in the constructor, or is it better to simply create something like
    Java Code:
    class MyGUI{
    and then use a method to set up the gui and do everything?

    Also, when it comes to action handling, I have seen that you can do
    Java Code:
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    and then use
    Java Code:
    if(e.getSource() == button1){
      //handle button 1 actions
    }
    else if(e.getSource() == button2){
      //handle button 2 actions
    }
    else if(e.getSource() == button3){
      //handle button 3 actions
    }
    The way I had been doing it is with inner classes implementing action listener and adding the inner class as the argument to
    Java Code:
    addActionListener
    Java Code:
    code
    inner class:
    private class Button1Listener implements ActionListener{
      //button1 action handling
    }
    private class Button2Listener implements ActionListener{
      //button2 action handling
    }
    private class Button3Listener implements ActionListener{
      //button3 action handling
    }
    Java Code:
    button1.addActionListener(new Button1Listener());
    button2.addActionListener(new Button2Listener());
    button3.addActionListener(new Button3Listener());
    Thanks in advance for any help.
    Last edited by sunde887; 02-15-2011 at 02:16 AM.

  2. #2
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Default serialization and gui

    In your first question about serialization you first need to understand why to serialize ?

    Serialization is used to save the objects in any stream in their persistent state.
    So if you want to do so your object should have the characteristics of the serialization so you need to implement the interface sirializable to that class and then you can take object of objectoutput stream and pass any stream in to it and then writeobject method will write that object in that particular stream
    eg.

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("filename"));
    oos.writeObject(object);


    In gui both the way are same either you extend Jframe or create the object of Jframe in java,
    but as the perspective of understanding the way to create an Object of JFrame is better one like follow

    Java Code:
    public class tempgui
    {
         static JFrame f1;
        public static void main(String a[])
        {
               f1=new JFrame("this is frame");
              JButton btn1=new JButton("click here");
    
            f1.getContentPane().add(btn1);
            f1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            btn1.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e)
                {
    
                    JOptionPane.showMessageDialog(f1,"You clicked the button");
                }
    
    
        });
            f1.setVisible(true);
        }
    }
    From my point of view to create inner class for action listener is better than implementing ActionListener interface..
    Last edited by Fubarable; 02-15-2011 at 04:50 AM. Reason: Moderator edit: code tags added

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator edit: code tags added to hemant_metalia's useful post.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Thanks for your contributions!!

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, I understand serialization, it's just I am going to be wanting to saving very large amounts of objects. I may eventually switch to a database when I know how, however for now, I'd prefer serialization. I am storing all the objects in a vector, and when the save button is clicked it will loop through the vector and serialize each item of the vector to the file. If I re-save over and over will it duplicate the items each time? Thus when I load it will have multiple duplicates of some items depending on how much I saved?

    as far as the gui stuff I was really just seeing which is a better practice to use.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im running into some buggy behavior with my code, I have a save listener which serializes a vector of items to a file name, here is the code:
    Java Code:
    save.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				if(theFile == null){
    					JFileChooser fc = new JFileChooser();
    					int retVal = fc.showSaveDialog(frame);
    					if(retVal == JFileChooser.APPROVE_OPTION){
    						theFile = fc.getSelectedFile();
    					}
    				}
    				else{
    					return;
    				}
    				try{
    					FileOutputStream fos = new FileOutputStream(theFile);
    					ObjectOutputStream oos = new ObjectOutputStream(fos);
    					for(DataCard dc : entries){
    						oos.writeObject(dc);
    					}
    					oos.close();
    				} catch(IOException ex){
    					ex.printStackTrace();
    				}
    			}
    		});
    This is a regular save and has expected functionality, if you haven't chosen a file to save to, it prompts for you to save some where then it saves, if you do have somewhere to save it simply saves to the correct file. It doesn't work correctly in one situation however.

    I defined a new menu item which prompts if you want to save and either saves, or doesn't then clears the gui, when I use save then new and choose no and then open the file it loses changes made before the save. If I choose yes on the box it works as expected and reflects the changes. Any ideas why this is happening? Here is the newlistener codes
    Java Code:
    newMen.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				if(entries.isEmpty() == false){
    					//JOptionPane thePopup = JOptionPane();
    					int choice = JOptionPane.showConfirmDialog(
    							null, 
    							"Would you like to save?", 
    							"Save?",
    							JOptionPane.YES_NO_OPTION);
    					if(choice == JOptionPane.YES_OPTION){
    						if(theFile == null){
    							JFileChooser fc = new JFileChooser();
    							int retVal = fc.showSaveDialog(frame);
    							if(retVal == JFileChooser.APPROVE_OPTION){
    								theFile = fc.getSelectedFile();
    							}
    						}
    						try{
    							FileOutputStream fos = new FileOutputStream(theFile);
    							ObjectOutputStream oos = new ObjectOutputStream(fos);
    							for(DataCard dc : entries){
    								oos.writeObject(dc);
    							}
    							oos.close();
    						} catch(IOException ioe){
    							ioe.printStackTrace();
    						}
    						entries.clear();
    						view.setListData(entries);
    					}
    					if(choice == JOptionPane.NO_OPTION){
    						entries.clear();
    						view.setListData(entries);
    					}
    				}
    			}
    		});
    Just to give some more idea here is the open code
    Java Code:
    open.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				JFileChooser fc = new JFileChooser();
    				int retVal = fc.showOpenDialog(frame);
    				entries.clear();
    				if(retVal == JFileChooser.APPROVE_OPTION){
    					theFile = fc.getSelectedFile();
    					try{
    						FileInputStream fis = new FileInputStream(theFile);
    						ObjectInputStream ois = new ObjectInputStream(fis);
    						while(true){
    							DataCard dc = (DataCard)ois.readObject();
    							entries.add(dc);
    						}
    					//ois.close();
    					} catch(EOFException eof){
    						view.setListData(entries);
    						return;
    					} catch(IOException ex){
    						ex.printStackTrace();
    					} catch(ClassNotFoundException ex1){
    						ex1.printStackTrace();
    					}
    				}
    			}
    		});

Similar Threads

  1. Serialization
    By twiggy62 in forum New To Java
    Replies: 2
    Last Post: 02-12-2010, 04:45 AM
  2. serialization
    By elsa14290 in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2010, 04:03 PM
  3. Serialization
    By thayalan in forum Advanced Java
    Replies: 4
    Last Post: 08-03-2009, 10:22 PM
  4. about serialization
    By bishnu in forum New To Java
    Replies: 0
    Last Post: 12-19-2008, 09:13 AM
  5. Replies: 0
    Last Post: 04-04-2008, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •