Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Serializing object to disc. Only retreives one object.

Serializing object to disc. Only retreives one object.

Scheduled Pinned Locked Moved Java
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L larsp777

    Ok, I'll try again. This is my total code in a class that inherits from JFrame instantiated from main(). Slightly changed because I come from Sweden. So I enter the info in the textfield for title, author and price. If I enter the info for one book and then press "show books" it works. I enter another book and press show books again it still works. The textarea shows all books. If I enter a book and doesn't press "show books", enter a few others and then hit "show books" it only shows the last entered book. If I have entered a few books using the first method (enter book, press "show books", enter another book, press show books again)and then quits using the Close-button one books is saved, not more.

    import javax.swing.BorderFactory;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.border.Border;
    import javax.swing.border.EtchedBorder;

    /*
    import java.awt.BorderLayout;
    import java.awt.Color;*/

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;

    public class MainFrame extends JFrame implements ActionListener, MyContacts {

    /\*\*
     \* 
     \*/
    private static final long serialVersionUID = 1L;
    public JTextField txtFieldTitle;
    private JTextField txtFieldAuthor;
    private JTextField txtFieldPrice;
    private JTextField txtFieldLanguage;
    private JTextArea ta1;
    private JButton btn;
    private JButton btn2;
    private JButton btnClose;
    private JButton btnRemove;
    private JRadioButton proRadio;
    private JRadioButton amateurRadio;
    private ButtonGroup playersBtnGroup;
    private ArrayList aList;
    //public ArrayList ProgrammingList;
    private JLabel txtTextArea;
    private JLabel txtTitle;
    private JLabel txtAuthor;
    private JLabel txtPrice;
    private Border redBorder = BorderFactory.createLineBorder(Color.red);
    
    public MainFrame(){ //Construktor
    
    	super("Library");
    	
    	setLayout(null);
    
    	//Sätter storlek på fönstret.
    	setSize(1200, 1000);
    	
    	//Hindra att fönstret kan ändras
    	setResizable(false);
    
    	
    	setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
    
    	
    	setVisible(true);
    							 
    	getC
    
    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #6

    if(e.getSource() == btn) //add Book
    {
    String title = txtFieldTitle.getText();
    // ...
    book aBok = new book(title, anAuthor , thePrice);
    aList = new ArrayList();
    aList.add(aBok);
    }//if source btn end

    Every time you add a book you create a new ArrayList(), so you are losing the previous books. You need to create your list at the beginning of the program and add to that as you enter more books.

    L 1 Reply Last reply
    0
    • L Lost User

      if(e.getSource() == btn) //add Book
      {
      String title = txtFieldTitle.getText();
      // ...
      book aBok = new book(title, anAuthor , thePrice);
      aList = new ArrayList();
      aList.add(aBok);
      }//if source btn end

      Every time you add a book you create a new ArrayList(), so you are losing the previous books. You need to create your list at the beginning of the program and add to that as you enter more books.

      L Offline
      L Offline
      larsp777
      wrote on last edited by
      #7

      Ah, of course! Thank you, so simple :) So, again, thank you very much! So can I mark as solved or give you credit?

      L 1 Reply Last reply
      0
      • L larsp777

        Ah, of course! Thank you, so simple :) So, again, thank you very much! So can I mark as solved or give you credit?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #8

        You can upvote my answer if you like. But I do this as a hobby, so your message of thanks is enough.

        L U 2 Replies Last reply
        0
        • L Lost User

          You can upvote my answer if you like. But I do this as a hobby, so your message of thanks is enough.

          L Offline
          L Offline
          larsp777
          wrote on last edited by
          #9

          Well, are very greatful. Don't really know how to upgrade, could you tell me? Also, I have another question and hope it is ok to ask it here. When I start the window it is blank or only a few items. When I minimize the window and maximaze it again, everything is fine. Do you know why this is?

          L 1 Reply Last reply
          0
          • L larsp777

            Well, are very greatful. Don't really know how to upgrade, could you tell me? Also, I have another question and hope it is ok to ask it here. When I start the window it is blank or only a few items. When I minimize the window and maximaze it again, everything is fine. Do you know why this is?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #10

            larsp777 wrote:

            Don't really know how to upgrade, could you tell me?

            Er, upgrade what?

            larsp777 wrote:

            When I start the window it is blank or only a few items.

            Sounds like your startup code is not forcing all items to be painted; suggest you open a new question with all the details.

            L 1 Reply Last reply
            0
            • L Lost User

              larsp777 wrote:

              Don't really know how to upgrade, could you tell me?

              Er, upgrade what?

              larsp777 wrote:

              When I start the window it is blank or only a few items.

              Sounds like your startup code is not forcing all items to be painted; suggest you open a new question with all the details.

              L Offline
              L Offline
              larsp777
              wrote on last edited by
              #11

              I meant how can I upgrade your answer to give you credit :) Sure I can start a new question if you want. All I do though is call the class I shown in main like this: new MainFrame(); And then I inherit from the main window like: public class MainFrame extends JFrame implements ActionListener { So most code is in the constructor. So should I post a new question?

              L 1 Reply Last reply
              0
              • L larsp777

                I meant how can I upgrade your answer to give you credit :) Sure I can start a new question if you want. All I do though is call the class I shown in main like this: new MainFrame(); And then I inherit from the main window like: public class MainFrame extends JFrame implements ActionListener { So most code is in the constructor. So should I post a new question?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #12

                larsp777 wrote:

                how can I upgrade your answer

                Sorry, I misunderstood, as the word "upgrade" has a slightly different meaning. If you open any message and move your mouse just to the left of the text near the top, you should see two coloured arrows. A green one pointing up, and a red one pointing down. Click the green one to upvote the message, or the red one to downvote (means you think it is not a good message). As to your other problem, I think we need to see the code, so I suggest you open a new question.

                L 1 Reply Last reply
                0
                • L Lost User

                  larsp777 wrote:

                  how can I upgrade your answer

                  Sorry, I misunderstood, as the word "upgrade" has a slightly different meaning. If you open any message and move your mouse just to the left of the text near the top, you should see two coloured arrows. A green one pointing up, and a red one pointing down. Click the green one to upvote the message, or the red one to downvote (means you think it is not a good message). As to your other problem, I think we need to see the code, so I suggest you open a new question.

                  L Offline
                  L Offline
                  larsp777
                  wrote on last edited by
                  #13

                  I meant upvote, sorry. For my other question I think I shown all code, but can post if again of course. Anyway I think I found a solution. Added this: repaint(); revalidate(); Seem to work. Not sure if the order matters.

                  1 Reply Last reply
                  0
                  • L Lost User

                    You can upvote my answer if you like. But I do this as a hobby, so your message of thanks is enough.

                    U Offline
                    U Offline
                    User 12078208
                    wrote on last edited by
                    #14

                    Whoa! before i thought i know Java but when i saw your code about serialization i realized i need to study more. I have never come across it. may be because I am just at intermediate level. your organization of class is what so much impress me about your codes. thank you so much. I wish I can get the complete source code for this.

                    L 1 Reply Last reply
                    0
                    • U User 12078208

                      Whoa! before i thought i know Java but when i saw your code about serialization i realized i need to study more. I have never come across it. may be because I am just at intermediate level. your organization of class is what so much impress me about your codes. thank you so much. I wish I can get the complete source code for this.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #15

                      There is no "complete source code", you have to write your own implementations. However, there are plenty of samples and tutorials that Google will find for you. [edit] See java serialization - Google Search[^] [/edit]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups