0

First post for me. I'm a student in my first year of a comp science degree.

I was just building a basic GUI for an assignment and for some reason, which I cannot see for the life of me, one of the JFrames I have created, an instance of the Register Class displays completely blank when it is called from the Register buttons action listener in the Login Class...

I also have an individual main Class which contains the Main method and calls the Login Class. The Login Class JFrame works fine and as mentioned, the issues only occur with the Registration Class when it is called within the Login Class' Register button action listener. All other JFrames in the program work fine too.

I have attempted to call the Register Class direct from the main and it has the same issues, although I have attempted to reduce it to its most basic form and it still does not display anything aside from a blank uncolored JFrame.

Here is the code (Unfinished but working as is). I apologise for my sloppyness but I am a complete beginner.

Can anyone see what is wrong with it?

Thanks in advance!

    package guitest;

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;

    public class Register extends JFrame{

        JButton regSubmit = new JButton("Submit");

        JTextField email = new JTextField();
        JTextField name = new JTextField();
        JTextField Address1 = new JTextField();
        JTextField Address2 = new JTextField();

        JPasswordField password1 = new JPasswordField();
        JPasswordField password2 = new JPasswordField();

        String nameTxt = email.getText();
        String passTxt = password1.getText();
        String info = "";

        FlowLayout layout1 = new FlowLayout();    

        public void Reg(){
            this.setTitle("La Volpe Registration");
            this.setLayout(layout1);    
            this.add(Address1);
            this.add(Address2);
            this.add(email);
            this.add(password1);
            this.add(password2);
            this.add(name);
            this.add(regSubmit);
            this.getContentPane().setBackground(Color.green);
            this.setSize(370, 160);

            regSubmit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    regSubmitActionPerformed(evt);
                }

                private void regSubmitActionPerformed(java.awt.event.ActionEvent evt) { 

                String name = email.getText();
                String pass = password1.getText();
                String info = "";

                System.out.println("registering...");

                boolean eof;

                try{
                    // Create file
                    FileWriter file = new FileWriter("\\regdata.txt");
                    BufferedWriter out = new BufferedWriter(file);
                    out.write("\n"+nameTxt+", "+passTxt);
                }

                catch (Exception e){
                }   
            }   
        });
        this.setVisible(true);
    }    
}

And the class that links to it...


package guitest;

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;

    /**
    * @author david
    */

    public class Login {

    JFrame loginFrame = new JFrame();
    Register reg3 = new Register();

    JButton submit = new JButton("Submit");
    JButton clear = new JButton("Clear");
    JButton register = new JButton ("Register with Us");

    JPasswordField pass = new JPasswordField(20);
    JTextField email = new JTextField(20);
    JLabel em = new JLabel("Email Address: ");
    JLabel pw = new JLabel("Password: ");

    String pathname;
    String line;
    String [] records = new String [1000];
    int count = 0;

    FlowLayout layout1 = new FlowLayout();

        public Login(){

            //Adds Email label and text field
            loginFrame.add(em);
            loginFrame.add(email);

            //Adds password label and field
            loginFrame.add(pw);
            loginFrame.add(pass);

            //Adds buttons
            loginFrame.add(submit);
            loginFrame.add(clear);
            loginFrame.add(register);

            loginFrame.getContentPane().setBackground(Color.green);

            loginFrame.setLayout(layout1);

            loginFrame.setSize(370, 160);

            loginFrame.setTitle("La Volpe - Login");

            submit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    submitActionPerformed(evt);    
                }            

                private void submitActionPerformed(java.awt.event.ActionEvent evt){                                         
                String emailadd = email.getText();
                String password = pass.getText();
                pathname = "\\regdata.txt";

                boolean eof;

                try{
                    FileReader file = new FileReader(pathname);
                    BufferedReader buff = new BufferedReader(file);

                    eof = false; // set the eof boolean to false

                        while (!eof){
                        line = buff.readLine();
                        if (line == null){ // test to see if the end of file has been reached
                        eof = true;  // if the end of file has been found set the eof Boolean to true
                        }
                        else{ 
                        // end of file not reached so move the contents of the line to the records
                        //array
                        records[count] = line;
                        count ++;
                        System.out.println(line);  // print out the new line input for error checking
                        }
                    }    
                    buff.close();
                }
                catch (IOException e){
                    System.out.println("Error --  "+ e.toString());
                }

                boolean notTrue = false;

                for (int i = 0; i < count; i++) {       
                    if ((!notTrue)&&((emailadd + "," + password).equals(records[i]))) {
                        FoodSelectionMain loggedIn = new FoodSelectionMain();
                        loggedIn.setVisible(true);
                    } 
                }
                if (!notTrue){
                    JOptionPane.showInputDialog("Please check your login "
                    + "and try again. If you are a new user, please "
                    + "register by pressing the 'REGISTER' button");
                }
            }   

        }); 

        // TODO add your handling code here:


        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }

            public void clearActionPerformed(java.awt.event.ActionEvent evt){
                email.setText(null);
                pass.setText(null);
            }

        }); 
        register.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerActionPerformed(evt);
            }

            public void registerActionPerformed(java.awt.event.ActionEvent evt){
                reg3.setVisible(true);
                    System.out.println("Register pressed");
            }
        });
        loginFrame.setVisible(true);
    }
}
diestl
  • 2,020
  • 4
  • 23
  • 37
DBIT
  • 182
  • 2
  • 11
  • 1
    No mention of `SwingUtilities`, no call to `pack()`. I'm not surprised it fails. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 22 '12 at 03:45
  • 2
    I commend you on your very creative use of indentation and white space, but this same creativity makes it very hard for others (namely those of us who would want to help you) to easily read and understand your code. You are asking for free advice from strangers after all, and so it's not being rude on our part to ask you to put in just a little bit of effort to not make it so difficult for us to help you. best of luck. – Hovercraft Full Of Eels May 22 '12 at 03:45
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 22 '12 at 03:46
  • I apologise for the sloppyness again. I'm new to programming and new to the site, so wasnt sure how best to lay it out. – DBIT May 22 '12 at 03:51
  • 1
    Try to copy the code style that you've seen here and in your assignments. One key is *unifomity* in everything including indentation. Another is scoping -- the deeper the scope the greater the indentation. But regardless, please do *something*. You can and should edit your text. This is your formal request for us to help you -- make it a request that others will want to reply to. – Hovercraft Full Of Eels May 22 '12 at 04:04
  • Thats understandable. I will from now on. Thanks guys. I have managed to fix the problem now so this can be closed. After getting a good nights rest I realised that there wasnt a valid constructor in the Reg Class. - Thanks for your time all. – DBIT May 22 '12 at 15:23

1 Answers1

1

Try this, in Register class, correct the constructor name to Register() from Reg().

Keep these few Guidelines in your mind before constructing a gui app

  1. Create an object of the subclass of the container .

  2. Consider all the components which goes in the container as instance variables.

  3. Set these instance variable and event handling outside the constructor in methods, (ie setComponent(), setHandler().etc) but do these method invocations from constructor.

  4. Now in main use this...

.

EventQueue.invokeLater(new Runnable() {

    public void run() {
        Myframe f = new Myframe();
        f.setVisible(true);
    }
}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • `extend JFrame` is reason why your answer haven't any upvotes – mKorbel May 22 '12 at 06:45
  • I completely forgot about this post. Sorry guys! I've just come back to the site after a year of hell with my college. If you would like me to tidy anything up please let me know, otherwise this can be closed. Thanks for the assistance, all the answers were appreciated. – DBIT Jul 07 '13 at 05:52