1

I have been using the "Learning Java 2nd Edtion" book to try make my java application write my input to a text file called properties. I have manipulated the text book example into my own code but still having problems trying to get it to work. I think i may need to hook it up to my submit button but this wasnt mentioned within the chapter.

Basically im trying to store the information in the text file and then use that text file within another location to read all of the property details.

Here is my code for the AddProperty Page so far any advice would be greatly appreciated. At the moment iv hit the wall.

/**
 *
 * @author Graeme
 */
package Login;

import java.io.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;

public class AddProperty
{
public void AddProperty()
{

    JFrame frame = new JFrame("AddPropertyFrame");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // having to set sizes of components is rare, and often a sign    
    // of problems with layouts.
    //frame.setSize(800,600);
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
    // make it big like the original
    panel.setBorder(new EmptyBorder(100,20,100,20));
    frame.add(panel);
    //panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    JLabel HouseNumber = new JLabel("House Number/Name");
    panel.add(HouseNumber);
    JTextField HouseNumber1 = new JTextField(10);
    panel.add(HouseNumber1);

    JLabel HousePrice = new JLabel("House Price");
    panel.add(HousePrice);
    JTextField HousePrice1 = new JTextField(10);
    panel.add(HousePrice1);

    JLabel HouseType = new JLabel("House Type");
    panel.add(HouseType);
    JTextField HouseType1 = new JTextField(10);
    panel.add(HouseType1);

    JLabel Location = new JLabel("Location");
    panel.add(Location);
    JTextField Location1 = new JTextField(10);
    panel.add(Location1);

    JButton submit = new JButton("Submit");
    panel.add(submit);
    submit.addActionListener(new Action());

    // tell the GUI to assume its natural (minimum) size.
    frame.pack();
}

static class Action implements ActionListener{

    @Override
    public void actionPerformed (ActionEvent e)
    {
        // this should probably be a modal JDialog or JOptionPane.
       JOptionPane.showMessageDialog(null, "You have successfully submitted a property.");
    }

    static class propertyList 
    {
        public static void main (String args[]) throws Exception {
            File properties = new File(args[0]);

            if (!properties.exists() || !properties.canRead() ) {
                System.out.println("Cant read " + properties);
                return;
            }
            if (properties.isDirectory()){
                String [] properties1 = properties.list();
                for (int i=0; i< properties1.length; i++)
                    System.out.println();
            }
            else
                try {
                    FileReader fr = new FileReader (properties);
                    BufferedReader in = new BufferedReader (fr);
                    String line;
                    while ((line = in.readLine())!= null)
                    System.out.println(line);
                }
            catch (FileNotFoundException e){
                System.out.println("Not Able To Find File");
            }
        }
    }
}

}

MPelletier
  • 16,256
  • 15
  • 86
  • 137

2 Answers2

2

in your Action performed you are not stating anything, for example in your action performed you could add.

   public void actionPerformed(ActionEvent e)
            {

              houseNumber2 = houseNumber1.getText();
              housePrice2 = housePrice1.getText();
              town1 = town.getText();
              comboBoxType2 = comboBoxType1.getSelectedItem();


            inputData = housenumber2 + "," + housePrice2 + "," + town1 + "," + comboBoxType2;
             FileName.Filewritermethod(inputData);
             frame.setVisible(false);

            }
       });

This would strings to take the values of your JTexFields and pass them onto a textfile provided you have a FileWriter Class

Darren Burgess
  • 4,200
  • 6
  • 27
  • 43
1

Your action listener is not doing anything much right now.

Add code to add property in the following method:

public void actionPerformed (ActionEvent e)
{
    //add your code here
}
Swaranga Sarma
  • 13,055
  • 19
  • 60
  • 93
  • i didnt think it was. so basicly i need to try link my button to the writing method? – Graeme Pearson May 21 '11 at 12:56
  • I already have a public void actionPerformed (ActionEvent e) Statement that dosnt really contain much at the moment. If i try and move my writing code into that section it comes up with an "illeagle starting expression" how do i get past this? – Graeme Pearson May 21 '11 at 13:02
  • 1
    @Graeme: if you're getting that error, then you're writing bad code, perhaps code outside of a method, perhaps... but why make us guess? ... **Please show your code.** – Hovercraft Full Of Eels May 21 '11 at 13:11
  • iv put my code back to how it was displayed above. Just trying to find out how to submit my form (made from JLabels and JTextFeilds) to the properties.txt file. At present i have no errors but its not coded correctly. thanks – Graeme Pearson May 21 '11 at 13:18
  • Looked at many examples on the internet and it seems that people write to text files in many different ways. i have used this one as i am new to java and this is the book recommended by my lectures. kind of clueless to what needs to be done to get it to work. can anyone advise me please? – Graeme Pearson May 21 '11 at 13:29
  • I bet the textbook doesn't recommend pasting random examples from the internet into the middle of your source code. I advise you to read the chapters of textbook in order, and do the exercises. – Stephen C May 21 '11 at 14:28