Click here to Skip to main content
15,888,251 members
Home / Discussions / Java
   

Java

 
GeneralRe: 3 x 3 plane,clockwise or counterclockwise rotate the four tiles Pin
Richard MacCutchan20-Jun-16 4:00
mveRichard MacCutchan20-Jun-16 4:00 
GeneralRe: 3 x 3 plane,clockwise or counterclockwise rotate the four tiles Pin
zarkerBlack21-Jun-16 3:17
zarkerBlack21-Jun-16 3:17 
GeneralRe: 3 x 3 plane,clockwise or counterclockwise rotate the four tiles Pin
Richard MacCutchan21-Jun-16 3:25
mveRichard MacCutchan21-Jun-16 3:25 
QuestionSerializing object to disc. Only retreives one object. Pin
larsp77714-Jun-16 1:42
larsp77714-Jun-16 1:42 
AnswerRe: Serializing object to disc. Only retreives one object. Pin
Richard MacCutchan14-Jun-16 3:47
mveRichard MacCutchan14-Jun-16 3:47 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
larsp77714-Jun-16 22:22
larsp77714-Jun-16 22:22 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
Richard MacCutchan14-Jun-16 22:47
mveRichard MacCutchan14-Jun-16 22:47 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
larsp77714-Jun-16 23:28
larsp77714-Jun-16 23:28 
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.

Java
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<book> aList;
	//public ArrayList<programmingBook> 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);
								 
		getContentPane().setBackground(Color.red);
		
		loadBook();
	
			
		//buttons
		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
		
			
		
		//Textfields
		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);
		
				
		
		//Textarea
		ta1 = new JTextArea();
		ta1.setBounds(720,470,400,420);
		ta1.setBorder(redBorder);
		add(ta1);
		
		
		
		//Labels
		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) //add Book
	{
		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 source btn end
		
	if(e.getSource() == btn2) //Show books
	{
			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 source btn2 end
		
		if(e.getSource() == btnClose)	//Close-button
		{
				saveBook(aList);
				System.exit(0);
		}
		
						
	}//ActionPerformed end
	
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");
    }
}//end saveBook	
	
@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...");
		}
	}//end loadBook	
	
	
	
}

GeneralRe: Serializing object to disc. Only retreives one object. Pin
Richard MacCutchan14-Jun-16 23:39
mveRichard MacCutchan14-Jun-16 23:39 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
larsp77714-Jun-16 23:52
larsp77714-Jun-16 23:52 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
Richard MacCutchan15-Jun-16 0:43
mveRichard MacCutchan15-Jun-16 0:43 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
larsp77716-Jun-16 21:28
larsp77716-Jun-16 21:28 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
Richard MacCutchan16-Jun-16 22:16
mveRichard MacCutchan16-Jun-16 22:16 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
larsp77716-Jun-16 22:43
larsp77716-Jun-16 22:43 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
Richard MacCutchan17-Jun-16 2:56
mveRichard MacCutchan17-Jun-16 2:56 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
larsp77717-Jun-16 12:48
larsp77717-Jun-16 12:48 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
Member 1211167124-Jun-16 20:57
Member 1211167124-Jun-16 20:57 
GeneralRe: Serializing object to disc. Only retreives one object. Pin
Richard MacCutchan24-Jun-16 21:02
mveRichard MacCutchan24-Jun-16 21:02 
Questionapplication architecture Pin
AppNewbie6-Jun-16 5:35
AppNewbie6-Jun-16 5:35 
AnswerRe: application architecture Pin
Richard MacCutchan6-Jun-16 6:39
mveRichard MacCutchan6-Jun-16 6:39 
Questionhow do i get IP address of the email sender's PC Pin
Member 125677045-Jun-16 23:43
Member 125677045-Jun-16 23:43 
AnswerRe: how do i get IP address of the email sender's PC Pin
Richard MacCutchan6-Jun-16 0:43
mveRichard MacCutchan6-Jun-16 0:43 
AnswerRe: how do i get IP address of the email sender's PC Pin
Richard Deeming6-Jun-16 4:54
mveRichard Deeming6-Jun-16 4:54 
QuestionWeb Questions Pin
tzungshian3-Jun-16 23:23
tzungshian3-Jun-16 23:23 
AnswerRe: Web Questions Pin
Richard MacCutchan4-Jun-16 1:08
mveRichard MacCutchan4-Jun-16 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.