|
I just ran a slightly modified version of your code and it returned all the items. I did find that the object reader did not return the list as an ArrayList<Book> (maybe my mistake) so had to modify the foreach to accept a List of objects and cast each one to a Book.
[edit]
A slight modification shows that your code should work fine, and return the full list.
[/edit]
modified 14-Jun-16 10:43am.
|
|
|
|
|
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<Book> bList = (ArrayList<Book>) oistr.readObject();
oistr.close();
for (Book bb : bList) {
bb.skrivUt();
}
which 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.*;
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<book> aList;
private JLabel txtTextArea;
private JLabel txtTitle;
private JLabel txtAuthor;
private JLabel txtPrice;
private Border redBorder = BorderFactory.createLineBorder(Color.red);
public MainFrame(){
super("Library");
setLayout(null);
setSize(1200, 1000);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
getContentPane().setBackground(Color.red);
loadBook();
btn = new JButton("Add book");
btn.setBounds(120,70,200,20);
add(btn);
btn.addActionListener(this);
btn2 = new JButton("Show books");
btn2.setBounds(220,470,200,20);
add(btn2);
btn2.addActionListener(this);
btnClose = new JButton("Close");
btnClose.setBounds(150,800,200,50);
add(btnClose);
btnClose.addActionListener(this); händelser
txtFieldTitle = new JTextField();
txtFieldTitle.setBounds(570,70,200,20);
add(txtFieldTitle);
txtFieldAuthor = new JTextField();
txtFieldAuthor.setBounds(570,110,200,20);
add(txtFieldAuthor);
txtFieldPrice = new JTextField();
txtFieldPrice.setBounds(570,150,200,20);
add(txtFieldPrice);
ta1 = new JTextArea();
ta1.setBounds(720,470,400,420);
ta1.setBorder(redBorder);
add(ta1);
txtTextArea = new JLabel("Böcker");
txtTextArea.setBounds(720,430,200,20);
add(txtTextArea);
txtTitle = new JLabel("Titel");
txtTitle.setBounds(500,70,200,20);
add(txtTitle);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn)
{
String title = txtFieldTitle.getText();
String anAuthor = txtFieldAuthor.getText();
String anPrice = txtFieldPrice.getText();
String aLanguage = txtFieldLanguage.getText();
int thePrice = Integer.parseInt(anPrice);
book aBok = new book(title, anAuthor , thePrice);
aList = new ArrayList<book>();
aList.add(aBok);
}
if(e.getSource() == btn2)
{
for (book b : aList) {
ta1.append("Titel " + b.getTitle() + "\n");
ta1.append("Författare: " + b.getAuthor() + "\n");
ta1.append("Pris: " + Integer.toString(b.getPrice()) + "\n");
ta1.append("\n");
}
}
if(e.getSource() == btnClose)
{
saveBook(aList);
System.exit(0);
}
}
public void saveBook(ArrayList<book> aList)
{
try
{
FileOutputStream fil = new FileOutputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectOutputStream oostr = new ObjectOutputStream(fil);
oostr.writeObject(aList);
oostr.close();
}
catch (IOException e)
{
System.out.println(e);
System.out.println("Nu blev det visst fel i spara Bok");
}
}
@SuppressWarnings("unchecked")
public void loadBook()
{
try
{
FileInputStream fil = new FileInputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectInputStream oistr = new ObjectInputStream(fil);
aList = (ArrayList<book>) oistr.readObject();
oistr.close();
}
catch (IOException e)
{
System.out.println(e);
System.out.println("Something went wrong in IO");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
System.out.println("Could not find class...");
}
}
}
|
|
|
|
|
if(e.getSource() == btn)
{
String title = txtFieldTitle.getText();
book aBok = new book(title, anAuthor , thePrice);
aList = new ArrayList<book>();
aList.add(aBok);
}
Every time you add a book you create a new ArrayList<book>() , 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?
|
|
|
|
|
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?
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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]
|
|
|
|
|
Hi guys,
The app needs to have drag and drop function to build tool with workflow like visio for business users. Easy access for members via web or mobile devices. Internet may not available but mobile can sync data when internet is available.
I'm setting up new application with the following architecture, so need your advice.
html5+JS on frontend on web, and swift on mobile.
Java - MVC webservices provide for both web and mobile front end.
bigdata on the back end.
you know any code generation should be used for Java?
Thanks,
AppNewbie
|
|
|
|
|
|
hello guys, i'm new in java language but what am thinking is about is it possible to get the ip address of the email sender machine?
if it is, how do the one can perform it in java?
|
|
|
|
|
|
Looking at the headers is the only way to do this.
However, bear in mind that the headers can easily be spoofed. Each server in the chain adds it's own Received header to indicate which computer it received the message from, but it has no way to verify the previous Received headers. As you trace the path back from your server to the sender, once you get beyond the servers that you trust, the headers could be entirely fictitious.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello everyone:
May I ask Java Web questions in there??
Thank you very much.
|
|
|
|
|
|
OK
Thank you very much!!!
|
|
|
|
|
I have the same question and thank you for the answer.
|
|
|
|
|
I have a jsp page with one browse button and one submit button.After selecting any excel file(.xlsx) when I click submit button, I want to store all the value of excel page to postgresql database
|
|
|
|
|
And what is your question?
|
|
|
|