Serializing object to disc. Only retreives one object.
-
Thanks for your reply. Could you show me some code? I tested to add some objects by writing in the textfields and pressing the button (btn). If I do this for some books and then hit the second button (btn2) it only shows the first book in the list. If I add a book and then hits the second button it show that book. If I add a second book and hits the second button again, it shows both books.
That is different from your original question. The code that works is the code I copied from your question, and the only changes I made are:
ArrayList bList = (ArrayList) oistr.readObject();
oistr.close();
for (Book bb : bList) {
bb.skrivUt();
// ta1.append("Titel " + b.getTitle() + "\n");
// ta1.append("Författare: " + b.getAuthor() + "\n");
// ta1.append("Pris: " + Integer.toString(b.getPrice()) + "\n");
// ta1.append("\n");
}//foreachwhich worked, and listed all the items I added to the original list. You are now asking something concerned with pressing buttons, which you have not shown in your code.
-
That is different from your original question. The code that works is the code I copied from your question, and the only changes I made are:
ArrayList bList = (ArrayList) oistr.readObject();
oistr.close();
for (Book bb : bList) {
bb.skrivUt();
// ta1.append("Titel " + b.getTitle() + "\n");
// ta1.append("Författare: " + b.getAuthor() + "\n");
// ta1.append("Pris: " + Integer.toString(b.getPrice()) + "\n");
// ta1.append("\n");
}//foreachwhich worked, and listed all the items I added to the original list. You are now asking something concerned with pressing buttons, which you have not shown in your code.
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
-
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
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 endEvery 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. -
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 endEvery 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. -
Ah, of course! Thank you, so simple :) So, again, thank you very much! So can I mark as solved or give you credit?
-
You can upvote my answer if you like. But I do this as a hobby, so your message of thanks is enough.
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?
-
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?
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.
-
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.
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?
-
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?
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.
-
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.
-
You can upvote my answer if you like. But I do this as a hobby, so your message of thanks is enough.
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.
-
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.