Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-18-2008, 04:01 PM
Member
 
Join Date: May 2008
Posts: 5
CompleteBeginner is on a distinguished road
Merging Ideas
Hi Everyone

Hopefully some of you can point out where Im going wrong.

I'm probably getting ahead of myself but thought Id try and merge a few ideas.

Concept im trying to achieve: Input Text in TextField, Stored in an object when click on save Jbutton, the saved text is then loaded from the object and displayed into a new textfield when load button is clicked.

I can then use this as a tempalte to learn and expand from an develop more complex programs that read and write data.

Anyway this is want I have so far

Code:
import javax.swing.*; import java.io.*; import java.util.*; import java.awt.event.*; import java.awt.*; public class Application extends JFrame implements ActionListener { public static void main(String[] arguments) { JTextField text = new JTextField("Input",6); JButton save = new JButton("Save"); JButton load = new JButton("Load"); JTextField output = new JTextField("0",6); public Application() { super("Sample Program"); setSize(400,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //add Listeners save.addActionListener(this); load.addActionListener(this); FlowLayout flow = new FlowLayout(FlowLayout.CENTER); setFlowLayout(flow); //add components add(text); add(save); add(load); add(output); setVisible(true); } } public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source == save) { Writer write = new Writer(); } else if (source == load) { Reader read = new Reader(); } } class Writer { Message mess = new Message(); String writetext = text.getText(); mess.writeMessage(writetext); try { FileOutputStream fo = new FileOutputStream("Message1.obj"); ObjectOutputStream oo = new ObjectOutputStream(fo); oo.writeObject(mess); oo.close(); } catch (IOException e) { System.out.println("Error - " + e.toString()); } } class Message implements Serializable { String text; void writeMessage(String inText); text = inText; } class Reader { try { FileInputStream fi = new FileInputStream("Message1.obj"); ObjectInputStream oi = new ObjectInputStream(fi); Message mess = (Message) oi.readObject(); output.setText(mess.text); oi.close(); } catch (Exception e) { System.out.println("Error - " e.toString()); } }
Im at 16 errors which is really bad. A few have been added since I tried to solve the original problem. Most are <identifier> expected errors.
My level of knowledge stands at I read through Sams Teach yourself Java 6 once and took a few notes but didnt really do many examples. i have spent a few days doing simple examples and now have tried to merge a few concepts.

Any help is greatly apprciated.

Last edited by CompleteBeginner : 05-18-2008 at 05:53 PM. Reason: missing main class
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-19-2008, 03:15 PM
Member
 
Join Date: May 2008
Posts: 5
CompleteBeginner is on a distinguished road
Code:
import javax.swing.*; import java.io.*; import java.util.*; import java.awt.event.*; import java.awt.*; public class Application extends JFrame implements ActionListener { JTextField text = new JTextField("Input",6); JButton save = new JButton("Save"); JButton load = new JButton("Load"); JTextField output = new JTextField("0",6); public Application() { super("Sample Program"); setSize(400,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //add Listeners save.addActionListener(this); load.addActionListener(this); FlowLayout flow = new FlowLayout(FlowLayout.CENTER); setLayout(flow); //add components add(text); add(save); add(load); add(output); setVisible(true); } public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source == save) { Writer(); } else if (source == load) { Reader(); } } void Writer() { Message mess = new Message(); String writetext = text.getText(); mess.writeMessage(writetext); try { FileOutputStream fo = new FileOutputStream("Message1.obj"); ObjectOutputStream oo = new ObjectOutputStream(fo); oo.writeObject(mess); oo.close(); } catch (IOException e) { System.out.println("Error - " + e.toString()); } } void Reader() { try { FileInputStream fi = new FileInputStream("Message1.obj"); ObjectInputStream oi = new ObjectInputStream(fi); Message mess = (Message) oi.readObject(); output.setText(mess.text); oi.close(); } catch (Exception e) { System.out.println("Error - " + e.toString()); } } public static void main(String[] arguments) { new Application(); } } class Message implements Serializable { String text; void writeMessage(String inText) { text = inText; } }

Last edited by CompleteBeginner : 05-19-2008 at 08:57 PM. Reason: Solved
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 07:34 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org