Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Was sent here to get some of code reviewed. Having difficulties with the problem. Wanted to know if I was going in the right direction. Here is the problem:

Create a Java GUI program which has one button, one text box and one textfield. When a user click on the button, the system should pop up a file selection window, which allows the user to select a file from the hard disk. After the file is selected, e.g., “ssn.txt”, your program should read input file “ssn.txt”. Inside the file each line represents a social security number. The program first read all the ssns into the memory. If the format of a ssn is wrong in the file, your program should throw a SSNException object, and catch that Exception object, and display an error message for that line in text box. You are expected to create the SSNException class which inherits the Exception class. It should contain a wrongSSN string. The constructor of SSNException should accept a string parameter (the line that you read from ssn.txt) which initializes the wrongSSN variable. It should also displays an error message with the wrong ssn string After it has read all the ssns, it will display all the valid ssns in the text box, one line for one ssn.

Here is a what I have done so far:

public class SSN_GUI extends JPanel implements ActionListener{

    public SSN_GUI(){

    makeObject();
    doTheLayout();
    theButton.addActionListener(this);

    }
    private JTextField inputSSN;
    private JButton theButton; 

    //Make all the Objects
    private void makeObject(){

        inputSSN = new JTextField(15);
        inputSSN.setEditable(true);
        theButton = new JButton();

        }

    //Layout all the Objects
    private void doTheLayout(){

        JPanel thePanel = new JPanel();
        thePanel.setLayout(new BorderLayout());
        thePanel.add(new JLabel("Enter Social Security Number: "));
        thePanel.add(inputSSN, "Center");
        thePanel.add(theButton, "North");
    }

    // Handle the button push
    public void actionPerformed(ActionEvent evt){
    }


}

public class SSNException extends Exception{
    private String wrongSSN;

    public SSNException(String data){
        wrongSSN = data;

    }

    }
share|improve this question
1  
What are the difficulties you are facing ? or do you want a general code review ? – NRS Nov 8 '11 at 8:23
shorter: JPanel thePanel = new JPanel(new BorderLayout()); – Landei Nov 8 '11 at 13:42
Do you want the code reviewed or do you want someone to help you debug it? Looks like the latter, in which case you'd be better off flagging it and asking for it to be migrated to StackOverflow. – Peter Taylor Nov 8 '11 at 13:47

2 Answers

Assuming you need a general code review

  1. I would start with following naming conventions. http://java.about.com/od/javasyntax/a/nameconventions.htm is a good place to start. There are many other similar articles
  2. I would name the methods and objects more sensibly so that the future Mister Bunker understands the code.

See this and this to get a better idea

share|improve this answer

First I would follow NRS advices: respect conventions naming, read some examples.

Second I would not extend my main class from JPanel nor from any JComponent subclass.

It seems you have problem about how to implement what is asked. I hope the pseudo-code below may help you to find the right direction:

//replace SSN_GUI with SsnGui to follow standard naming conventions:
public class SsnGui implements ActionListener{

    public static void main(...){
        ssnGui = new SsnGui();
        ssnGui.show();
    }
    //Attributes declaration should be before constructor
    ...
    private JFrame theFrame = null;
    private ArrayList listOfSSN = new ArrayList();
    private JTextArea theTextBox = null;

    public SsnGui(){
        makeObjectAndDoTheLayout();
        theButton.addActionListener(this);
    }

    ...
    //On single method instead of two (makeObject and doTheLayout)
    private void makeObjectAndDoTheLayout(){
        theButton = new JButton();

        ...
        theFrame = new JFrame(...);
        theFrame.setLocation(...);
        theFrame.setSize(...);
        theFrame.add(thePanel);
    }
    public void show(){
        theFrame.setVisible(true);
    }

    // Handle the button push
    public void actionPerformed(ActionEvent evt){
        try{
            //Display a JFileChooser
            ...
            //Get the file from the JFileChooser
            ...
            //Read the file using FileReader
            //Fill the listOfSSN arrayList
            //When encounter a bad SSN, throw a SSNException

            //Once file is read,
            //Generate a string using StringBuffer and looping through the
            //elements of the listOfSSN
            ...
            //Then fill the text box using setText method
            ...
        }
        catch(SSNException ex){
            //Display your error here inside a JOptionPane
            ...
        }
    }
}
share|improve this answer
I appreciate the help. Building GUI's has always been confusing to me. I've read up on them and followed certain tutorials but I am still having problems. The makeObjectAndDoTheLayout method has me confused. – Mister Bunker Nov 8 '11 at 22:38
Actually, it is not mandatory to use a makeObjectAndDoTheLayout method but you can do it into the constructor: the important thing is to declare the components of your GUI, to instantiate them and then to layout the whole GUI. After that, you can add the behaviour (the action listener). – lauhub Nov 9 '11 at 8:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.