0

Hi, am so new to java programming and I need help to create an address book. Am using this project to test my self.(http://sis.ashesi.edu.gh/courseware/cms/mod/resource/view.php?id=218). Please this is my writen code but I need help to add, edit, delete, and the rest of the functions. Or better if someone can guide me please.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.swing.SwingConstants;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;


public class AddressBook extends JFrame {
    private JTextField txtSearchHere;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
    private JTextField txtFileName;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddressBook frame = new AddressBook();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AddressBook() {
        setResizable(false);
        setTitle("AddressBook");
        setBounds(100, 100, 670, 560);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        JPanel topPanel = new JPanel();
        topPanel.setBounds(21, 28, 620, 58);
        getContentPane().add(topPanel);
        topPanel.setLayout(null);

        JLabel lblFileName = new JLabel("File Name");
        lblFileName.setFont(new Font("Myriad Web Pro", Font.PLAIN, 20));
        lblFileName.setBounds(10, 18, 93, 21);
        topPanel.add(lblFileName);

        txtFileName = new JTextField();
        txtFileName.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        txtFileName.setBounds(113, 13, 319, 32);
        topPanel.add(txtFileName);
        txtFileName.setColumns(10);

        JButton btnSave = new JButton("Save");
        btnSave.setBounds(533, 11, 77, 35);
        topPanel.add(btnSave);
        btnSave.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));

        JButton btnLoad = new JButton("Load");
        btnLoad.setBounds(452, 11, 77, 35);
        topPanel.add(btnLoad);
        btnLoad.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));

        JPanel contentPanel = new JPanel();
        contentPanel.setBackground(new Color(240, 248, 255));
        contentPanel.setForeground(Color.WHITE);
        contentPanel.setBorder(new LineBorder(Color.LIGHT_GRAY));
        contentPanel.setBounds(21, 97, 444, 312);
        getContentPane().add(contentPanel);
        contentPanel.setLayout(null);

        JLabel lblNewLabel = new JLabel("Surname:");
        lblNewLabel.setFont(new Font("Myriad Web Pro", Font.PLAIN, 20));
        lblNewLabel.setBounds(10, 24, 89, 22);
        contentPanel.add(lblNewLabel);

        textField = new JTextField();
        textField.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        textField.setBounds(156, 21, 275, 30);
        contentPanel.add(textField);
        textField.setColumns(10);

        JLabel lblOtherName = new JLabel("Other Names:");
        lblOtherName.setFont(new Font("Myriad Web Pro", Font.PLAIN, 20));
        lblOtherName.setBounds(10, 65, 126, 22);
        contentPanel.add(lblOtherName);

        JLabel lblAddress = new JLabel("Address:");
        lblAddress.setFont(new Font("Myriad Web Pro", Font.PLAIN, 20));
        lblAddress.setBounds(10, 106, 126, 22);
        contentPanel.add(lblAddress);

        JLabel lblBoxNumber = new JLabel("Box Number:");
        lblBoxNumber.setFont(new Font("Myriad Web Pro", Font.PLAIN, 20));
        lblBoxNumber.setBounds(10, 147, 126, 22);
        contentPanel.add(lblBoxNumber);

        JLabel lblCity = new JLabel("City:");
        lblCity.setFont(new Font("Myriad Web Pro", Font.PLAIN, 20));
        lblCity.setBounds(10, 192, 126, 22);
        contentPanel.add(lblCity);

        JLabel lblCountry = new JLabel("Country:");
        lblCountry.setFont(new Font("Myriad Web Pro", Font.PLAIN, 20));
        lblCountry.setBounds(10, 233, 126, 22);
        contentPanel.add(lblCountry);

        JLabel lblPhoneNumber = new JLabel("Phone Number:");
        lblPhoneNumber.setFont(new Font("Myriad Web Pro", Font.PLAIN, 20));
        lblPhoneNumber.setBounds(10, 274, 146, 22);
        contentPanel.add(lblPhoneNumber);

        textField_1 = new JTextField();
        textField_1.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        textField_1.setColumns(10);
        textField_1.setBounds(156, 62, 275, 30);
        contentPanel.add(textField_1);

        textField_2 = new JTextField();
        textField_2.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        textField_2.setColumns(10);
        textField_2.setBounds(156, 103, 275, 30);
        contentPanel.add(textField_2);

        textField_3 = new JTextField();
        textField_3.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        textField_3.setColumns(10);
        textField_3.setBounds(156, 144, 275, 30);
        contentPanel.add(textField_3);

        textField_4 = new JTextField();
        textField_4.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        textField_4.setColumns(10);
        textField_4.setBounds(156, 189, 275, 30);
        contentPanel.add(textField_4);

        textField_5 = new JTextField();
        textField_5.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        textField_5.setColumns(10);
        textField_5.setBounds(156, 230, 275, 30);
        contentPanel.add(textField_5);

        textField_6 = new JTextField();
        textField_6.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        textField_6.setColumns(10);
        textField_6.setBounds(156, 271, 275, 30);
        contentPanel.add(textField_6);

        JPanel btnPanel = new JPanel();
        btnPanel.setBorder(new LineBorder(Color.LIGHT_GRAY));
        btnPanel.setBackground(new Color(240, 248, 255));
        btnPanel.setBounds(475, 97, 166, 312);
        getContentPane().add(btnPanel);
        btnPanel.setLayout(null);

        //Buttons @ West Area
        JButton btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    File file = new File ("C:\\Users\\Delsoft\\Desktop\\Java Resource\\index.txt");
                    if (!file.exists()){
                        file.createNewFile();
                    }
                    //Define Source file location later and writing/saving to file
                    FileWriter writer = new FileWriter ("C:\\Users\\Delsoft\\Desktop\\Java Resource\\index.txt",  true);
                    BufferedWriter bw = new BufferedWriter (writer);


                    textField.write(bw);
                    textField_1.write(bw);
                    textField_2.write(bw);
                    textField_3.write(bw);
                    textField_4.write(bw);
                    textField_5.write(bw);
                    textField_6.write(bw);

                    bw.close();
                    textField.setText("");
                    textField_1.setText("");
                    textField_2.setText("");
                    textField_3.setText("");
                    textField_4.setText("");
                    textField_5.setText("");
                    textField_6.setText("");
                    textField.requestFocus();

                }           

                catch (Exception e){
                    JOptionPane.showMessageDialog(null,e);

                }
                    // successful in saving     
                JOptionPane.showMessageDialog(null,"Successfully Saved");


            }
        });

        btnAdd.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        btnAdd.setBounds(10, 18, 145, 38);
        btnPanel.add(btnAdd);

        JButton btnEdit = new JButton("Edit");
        btnEdit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                JOptionPane.showConfirmDialog(null,"Do you want to Edit selected Details");
            }
        });
        btnEdit.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        btnEdit.setBounds(10, 67, 145, 38);
        btnPanel.add(btnEdit);

        JButton btnUpdate = new JButton("Update");
        btnUpdate.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        btnUpdate.setBounds(10, 116, 145, 38);
        btnPanel.add(btnUpdate);

        JButton btnDelete = new JButton("Delete");
        btnDelete.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        btnDelete.setBounds(10, 165, 145, 38);
        btnPanel.add(btnDelete);

        JButton btnImport = new JButton("Import");
        btnImport.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        btnImport.setBounds(10, 214, 145, 38);
        btnPanel.add(btnImport);

        JButton btnExport = new JButton("Export");
        btnExport.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        btnExport.setBounds(10, 263, 146, 38);
        btnPanel.add(btnExport);

        JPanel panel = new JPanel();
        panel.setBounds(21, 420, 620, 58);
        getContentPane().add(panel);
        panel.setLayout(null);

        JLabel lblSearch = new JLabel("Search:");
        lblSearch.setBounds(10, 18, 67, 22);
        panel.add(lblSearch);
        lblSearch.setFont(new Font("Myriad Web Pro", Font.PLAIN, 20));

        //South Search Area
        txtSearchHere = new JTextField();
        txtSearchHere.setBounds(123, 15, 319, 30);
        panel.add(txtSearchHere);
        txtSearchHere.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));
        txtSearchHere.setHorizontalAlignment(SwingConstants.CENTER);
        txtSearchHere.setText("Search by Surname");
        txtSearchHere.setColumns(10);

        JButton btnEnter = new JButton("Search");
        btnEnter.setBounds(452, 11, 98, 35);
        panel.add(btnEnter);
        btnEnter.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));

        JButton btnSortBySurname = new JButton("Sort");
        btnSortBySurname.setBounds(552, 11, 67, 35);
        panel.add(btnSortBySurname);
        btnSortBySurname.setFont(new Font("Myriad Web Pro", Font.PLAIN, 18));

        //South Area
        JLabel lblVersion = new JLabel("Version: 0.0.1");
        lblVersion.setBounds(31, 512, 76, 14);
        getContentPane().add(lblVersion);
        lblVersion.setFont(new Font("Myriad Web Pro", Font.PLAIN, 9));
        lblVersion.setForeground(Color.GRAY);

        JPanel footerPanel = new JPanel();
        footerPanel.setBounds(21, 501, 620, 10);
        getContentPane().add(footerPanel);
        footerPanel.setBackground(new Color(255, 215, 0));

        JLabel lblPoweredByCs = new JLabel("Powered By: CS Class '16'");
        lblPoweredByCs.setBounds(517, 512, 113, 14);
        getContentPane().add(lblPoweredByCs);
        lblPoweredByCs.setFont(new Font("Myriad Web Pro", Font.PLAIN, 9));
        lblPoweredByCs.setForeground(Color.GRAY);

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(0, 0, 664, 21);
        getContentPane().add(panel_1);
        panel_1.setLayout(null);

        //Helper Function MenuBar
        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 664, 21);
        panel_1.add(menuBar);
        menuBar.setToolTipText("");

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmAddNew = new JMenuItem("Add New");
        mnFile.add(mntmAddNew);

        JMenuItem mntmSave = new JMenuItem("Save");
        mnFile.add(mntmSave);

        JMenuItem menuSaveAs = new JMenuItem("Save As");
        mnFile.add(menuSaveAs);

        JMenuItem mntmExit = new JMenuItem("Exit");
        mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(EXIT_ON_CLOSE);
            }
        });
        mnFile.add(mntmExit);

        JMenu mnHelp = new JMenu("Help");
        menuBar.add(mnHelp);

        JMenuItem mntmAbout = new JMenuItem("About");
        mnHelp.add(mntmAbout);

    }
}
2
Contributors
1
Reply
12
Views
3 Years
Discussion Span
Last Post by JamesCherrill
0

Welcome to DaniWeb!

People here will be happy to help you - but you'll get the best value from that if you ask specific questions.

Looking at your code there are a few small points of feedback that jump out...

You have a constructor that just goes on for hundreds of lines, which makes it almost unreadable and certainly unnavigable. Much better to break out sections into simple well-named methods so we can all see what's going on. Eg have a method for each of the panels, that returns a completed panel ready to be added to the JFRame.

You have text fields with maximally-unhelpful names. Much better to give them meaningful names for clarity and to avoid mistakes.

You use null layouts and pixel bounds. That's OK until someone else tries to run your program on their high-res screen, or their new iMac, when none of the text will fit and everybody will look enbarassed. Using layout managers is a bit of a learning curve, but they exist for a very good reason.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.