Results 1 to 2 of 2

Thread: Merging Ideas

  1. #1
    Join Date
    May 2008
    Posts
    5
    Rep Power
    0

    Default 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

    Java 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

  2. #2
    Join Date
    May 2008
    Posts
    5
    Rep Power
    0

    Default

    Java 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •