0

My program is showing that it is running successfully, but my dialogue box will not show up. I am new to Java and I am not sure where I am going wrong can someone help? I am trying to write to a file that will store the input from the user

/*


 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    package database;

/**
 *
 *
 */

   import java.io.*;
   import java.awt.*;
   import java.awt.event.*;

    `enter code here`import javax.swing.*;

    `public class Database extends JFrame implements ActionListener{

        FlowLayout flow = new FlowLayout();

        JLabel fName = new JLabel("First Name");
        JTextField fName1 = new JTextField(15);

         JLabel blankSpaces1 = new JLabel("                             ");

         JLabel lName = new JLabel("Last Name");
         JTextField lName1 = new JTextField(15);
         JLabel blankSpaces2 = new JLabel("                                                       ");

         JLabel age = new JLabel("Age");
        JTextField age1 = new JTextField(3);
      JLabel blankSpaces3 = new JLabel("                 ");

         JLabel email = new JLabel("Email Address");
         JTextField email1 = new JTextField(30);
         JLabel blankSpaces4 = new JLabel("                 ");

         JLabel phone = new JLabel("Cell Phone Number");
         JTextField phone1 = new JTextField(10);
         JLabel blankSpaces5 = new JLabel("                 ");

         JLabel blankSpaces6 = new JLabel("                 ");


         JButton enter = new JButton("Enter");
         JButton reset = new JButton("Reset");
         JButton exitButton = new JButton("Exit");
         Container con = getContentPane();


        public void Database (){
           Database Dab = new Database();
           con.setLayout(flow);
          setTitle("Antwain's Database");

        Dab.setSize(800,300);
        setVisible(true);





            con.add(fName);
            con.add(blankSpaces1);
            con.add(fName1);
            con.add(blankSpaces1);
            con.add(lName);
            con.add(lName1);
            con.add(blankSpaces2);

            con.add(age);
            con.add(age1);
            con.add(blankSpaces3);

            con.add(email);
            con.add(email1);
            con.add(blankSpaces4);

            con.add(phone);
            con.add(phone1);
            con.add(blankSpaces5);

            con.add(blankSpaces6);
            con.add(blankSpaces2);



            con.add(enter);
            con.add(exitButton);
            con.add(reset); 

        enter.addActionListener(this);
        exitButton.addActionListener(this);
        reset.addActionListener(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

            public void actionPerformed(ActionEvent e1){
          Object source = e1.getSource();

         if(source == enter){

            String fName = fName1.getText();
            String lName = lName1.getText();
            String age = age1.getText();
            String email = email1.getText();
            String phone = phone1.getText();

        }
        else
            if(source == reset){
            fName1.setText("");
            lName1.setText("");
            age1.setText("");
            email1.setText("");
            phone1.setText("");
    }
           else
              {
                // if the user clicks on the Exit button (source is Exit button)
                System.exit(0);
              } 


                   }



        public static void main(String[] args) {

           Database dab = new Database();

    }
        }
5
  • 3
    Please reduce your question's code to the amount relevant to the issue. Also, I can't find anything related to dialogs in your current code. Commented Dec 14, 2012 at 7:15
  • 2
    you should first learn formatting your code. Commented Dec 14, 2012 at 7:15
  • I don't see the code which writes a file. Write a method/Class to write to the file and test it with your input parameters. Test that method and see everything works fine. The use that method or Class in UI layer. Commented Dec 14, 2012 at 7:31
  • 1) For better help sooner, post an SSCCE. 2) Please use a consistent and logical indent for code blocks. 3) Dab.setSize(800,300); will likely achieve nothing. 4) setVisible(true); should be done last, just after a call to pack() 5) Don't extend frame, just use an instance. 6) Swing GUIs should be started on the EDT. See Concurrency in Swing for more details. Commented Dec 14, 2012 at 7:34
  • Please format your code. Dont start variables with capitals. make attributes private when possible. Remove void in your Database method. Remove Database Dab = new Database(); in your late method, now constructor (due to void removal). When using eclipse, use Source > Format to format your code automatically. Commented Dec 14, 2012 at 7:42

2 Answers 2

1

A lot more you need to go, fixed something you wrongly did in your code. Hope you can identify your mistakes.

public class DataBaseFrame extends JFrame implements ActionListener {

    FlowLayout flow = new FlowLayout();
    JLabel fName = new JLabel("First Name");
    JTextField fName1 = new JTextField(15);
    JLabel blankSpaces1 = new JLabel("");
    JLabel lName = new JLabel("Last Name");
    JTextField lName1 = new JTextField(15);
    JLabel blankSpaces2 = new JLabel("");
    JLabel age = new JLabel("Age");
    JTextField age1 = new JTextField(3);
    JLabel blankSpaces3 = new JLabel("");
    JLabel email = new JLabel("Email Address");
    JTextField email1 = new JTextField(30);
    JLabel blankSpaces4 = new JLabel("");
    JLabel phone = new JLabel("Cell Phone Number");
    JTextField phone1 = new JTextField(10);
    JLabel blankSpaces5 = new JLabel("");
    JLabel blankSpaces6 = new JLabel("");
    JButton enter = new JButton("Enter");
    JButton reset = new JButton("Reset");
    JButton exitButton = new JButton("Exit");

    public DataBaseFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Antwain's Database");
        JPanel panel = new JPanel(new FlowLayout());
        panel.add(fName);
        panel.add(blankSpaces1);
        panel.add(fName1);
        panel.add(blankSpaces1);
        panel.add(lName);
        panel.add(lName1);
        panel.add(blankSpaces2);

        panel.add(age);
        panel.add(age1);
        panel.add(blankSpaces3);

        panel.add(email);
        panel.add(email1);
        panel.add(blankSpaces4);

        panel.add(phone);
        panel.add(phone1);
        panel.add(blankSpaces5);

        panel.add(blankSpaces6);
        panel.add(blankSpaces2);

        panel.add(enter);
        panel.add(exitButton);
        panel.add(reset);
        getContentPane().add(panel);
        enter.addActionListener(this);
        exitButton.addActionListener(this);
        reset.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e1) {
        Object source = e1.getSource();

        if (source == enter) {

            String fName = fName1.getText();
            String lName = lName1.getText();
            String age = age1.getText();
            String email = email1.getText();
            String phone = phone1.getText();

        } else if (source == reset) {
            fName1.setText("");
            lName1.setText("");
            age1.setText("");
            email1.setText("");
            phone1.setText("");
        } else {
            // if the user clicks on the Exit button (source is Exit button)
            System.exit(0);
        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DataBaseFrame dab = new DataBaseFrame();
                dab.setSize(800, 300);
                dab.setVisible(true);
            }
        });

    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

new Database();
calls an empty Constructor, because you dont define none.
Change public void Database() {} to public Database()
Also, you have to eliminate Database Dab = new Database(); from your Constructor and change the Line Dab.setSize(800,300); to setSize(800,300);
Your Code will work when you make this changes but you should really work on your Programming Style and Code Formatting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.