hanntaa 0 Newbie Poster

hey guys i was wondering if someone could help, i have to create an address book in java that can add, delete, search as well as import details from a file.

so far iv got the GUI down (ish) iv set it over 2 frames, one to edit the data and containing alll the buttons and the other at the moment is just a JList containing the details such as name, address, phone number.

However, i cant get the jlist to take more then the name on a single line and the other details just get printed in the JList below each other.

am i using the wrong thing? maybe a JTextField or JTable? and would either of those be easyer when importing?

any help would be great thanks

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.text.*;





public class Q3 implements ActionListener
{
	
   	JFrame addressBook;   	
   	JFrame contacts;
   	JList theList;
   	DefaultListModel details;
   	
      	
   	JLabel labName, labAddress, labPhone,labMob;
   	JTextField textName, textAddress, textPhone, textMob, contactsTextArea;
   	JButton buttonSave, buttonDelete, buttonImport, buttonSearch, buttonForward, buttonBack, buttonExit;
    
       	
   	String name, address, allContacts;
   	int phone, mob;
   	int recordNumber;	 // used to naviagate using >> and << buttons 
   	Container container1;   	
   	JPanel panel1;
   	JMenuBar menu_bar;
   	JMenu file;
   	JMenuItem exit;
   	JMenuItem open;
   	JScrollPane scroller;
   	   	
   	
   	
   	public static void main(String args[])
   	
   	{
      new Q3(); 	  
    }
   	
   	public Q3()
    { 	
 	    name    = "";
 	    address = "";
 	    phone   = -1 ;		//Stores 0 to indicate no Phone Number
 	    mob = -1;
 	    recordNumber = -1;
 	
 	    createGUI();
 	    createContacts();
 	    
 	  	     	
 	}
   	
   	public void createContacts()
   	{
   		contacts = new JFrame("Contacts");        
   		contacts.setSize(200,300);                            
   		contacts.setVisible(true);
   		contacts.setResizable(false);
   		
   		panel1 = new JPanel();
   		contacts.getContentPane().add(panel1);
   		panel1.setBackground(Color.BLACK);
   		
   		menu_bar = new JMenuBar();
   		contacts.setJMenuBar(menu_bar);
   		file = new JMenu ("File");
   		menu_bar.add(file);
   		exit = new JMenuItem ("Exit");
   		file.add(exit);
   		exit.addActionListener(this);
   		open = new JMenuItem ("Open");
   		file.add(open);
   		open.addActionListener(this);
   		
   		
   		
   		contactsTextArea = new JTextField(20);
   		
   		panel1.add(contactsTextArea);
   		scroller = new JScrollPane(contactsTextArea);
   		panel1.getRootPane().add(scroller);
   		
   		
   		theList = new JList();
   		this.details = new DefaultListModel();
        this.details.addElement("Fred");
        this.details.addElement("Barney");
        this.details.addElement("Wilma");
        this.theList = new JList(this.details);
        panel1.add(this.theList);
        
   		   		 
   		
   	}
   	
	

	public void createGUI()
   	{

   		/*Create a frame, get its contentpane and set layout*/
   		addressBook = new JFrame("Address Book");
   		addressBook.setSize(300,300);
   		addressBook.setResizable(false);
   		addressBook.setVisible(true);
 		   		
   		container1 = addressBook.getContentPane();
   		container1.setLayout(new GridBagLayout());
   		container1.setBackground(Color.black);
   		container1.setSize(300, 300);
   		
   		menu_bar = new JMenuBar();
   		addressBook.setJMenuBar(menu_bar);
   		file = new JMenu ("File");
   		menu_bar.add(file);
   		exit = new JMenuItem ("Exit");
   		file.add(exit);
   		exit.addActionListener(this);
   		
   		  		
   		//Arrange components on contentPane and set Action Listeners to each JButton
   		arrangeComponents();   		   		   			

   	}
   	public void arrangeComponents()
   	{
   		
   		
   		labName = new JLabel("Name");
   		labName.setForeground(Color.white);
   		   		   		
   		labAddress = new JLabel("Address");
   		labAddress.setForeground(Color.white);
   		
   		labPhone = new JLabel("Home Number");
   		labPhone.setForeground(Color.white);
   		
   		labMob = new JLabel("Mobile Number");
   		labMob.setForeground(Color.white);   		
   		
   		textName    = new JTextField(20);
   		textAddress = new JTextField(20);
   		textPhone   = new JTextField(20);
   		textMob   = new JTextField(20);

   		buttonSave   = new JButton("Save");
   		buttonDelete = new JButton("Delete");
   		buttonSearch = new JButton("Search");
   		buttonImport = new JButton("Import");

   		buttonForward = new JButton("FORWARD");
   		buttonBack    = new JButton("BACK");
   		buttonExit    = new JButton("Exit");

   		/*add all initialized components to the container*/
   		GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
        gridBagConstraintsx01.gridx = 0;
        gridBagConstraintsx01.gridy = 0;
        gridBagConstraintsx01.insets = new Insets(5,5,5,5); 
        container1.add(labName, gridBagConstraintsx01);
        
        GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
        gridBagConstraintsx02.gridx = 1;
        gridBagConstraintsx02.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx02.gridy = 0;
        gridBagConstraintsx02.gridwidth = 2;
        gridBagConstraintsx02.fill = GridBagConstraints.BOTH;
        container1.add(textName, gridBagConstraintsx02);
        
        GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
        gridBagConstraintsx03.gridx = 0;
        gridBagConstraintsx03.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx03.gridy = 1;
        container1.add(labAddress, gridBagConstraintsx03);
        
        GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
        gridBagConstraintsx04.gridx = 1;
        gridBagConstraintsx04.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx04.gridy = 1;
        gridBagConstraintsx04.gridwidth = 2;
        gridBagConstraintsx04.fill = GridBagConstraints.BOTH;
        container1.add(textAddress, gridBagConstraintsx04);
        
        GridBagConstraints gridBagConstraintsx05 = new GridBagConstraints();
        gridBagConstraintsx05.gridx = 0;
        gridBagConstraintsx05.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx05.gridy = 2;
        container1.add(labPhone, gridBagConstraintsx05);
        
        GridBagConstraints gridBagConstraintsx06 = new GridBagConstraints();
        gridBagConstraintsx06.gridx = 1;
        gridBagConstraintsx06.gridy = 2;
        gridBagConstraintsx06.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx06.gridwidth = 2;
        gridBagConstraintsx06.fill = GridBagConstraints.BOTH;
        container1.add(textPhone, gridBagConstraintsx06);
        
        GridBagConstraints gridBagConstraintsx07 = new GridBagConstraints();
        gridBagConstraintsx07.gridx = 0;
        gridBagConstraintsx07.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx07.gridy = 3;
        container1.add(labMob, gridBagConstraintsx07);
        
        GridBagConstraints gridBagConstraintsx08 = new GridBagConstraints();
        gridBagConstraintsx08.gridx = 1;
        gridBagConstraintsx08.gridy = 3;
        gridBagConstraintsx08.gridwidth = 2;
        gridBagConstraintsx08.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx08.fill = GridBagConstraints.BOTH;
        container1.add(textMob, gridBagConstraintsx08);
        
        GridBagConstraints gridBagConstraintsx09 = new GridBagConstraints();
        gridBagConstraintsx09.gridx = 0;
        gridBagConstraintsx09.gridy = 4;
        gridBagConstraintsx09.insets = new Insets(5,5,5,5); 
        container1.add(buttonSave, gridBagConstraintsx09);
        
        GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
        gridBagConstraintsx10.gridx = 1;
        gridBagConstraintsx10.gridy = 4;
        gridBagConstraintsx10.insets = new Insets(5,5,5,5); 
        container1.add(buttonDelete, gridBagConstraintsx10);
        
        GridBagConstraints gridBagConstraintsx11 = new GridBagConstraints();
        gridBagConstraintsx11.gridx = 2;
        gridBagConstraintsx11.gridy = 4;
        gridBagConstraintsx11.insets = new Insets(5,5,5,5); 
        container1.add(buttonImport, gridBagConstraintsx11);
                
        GridBagConstraints gridBagConstraintsx12 = new GridBagConstraints();
        gridBagConstraintsx12.gridx = 0;
        gridBagConstraintsx12.gridy = 5;
        gridBagConstraintsx12.insets = new Insets(5,5,5,5);
        container1.add(buttonBack, gridBagConstraintsx12);
        
        GridBagConstraints gridBagConstraintsx13 = new GridBagConstraints();
        gridBagConstraintsx13.gridx = 1;
        gridBagConstraintsx13.gridy = 5;
        gridBagConstraintsx13.insets = new Insets(5,5,5,5); 
        container1.add(buttonSearch, gridBagConstraintsx13);
        
        GridBagConstraints gridBagConstraintsx14 = new GridBagConstraints();
        gridBagConstraintsx14.gridx = 2;
        gridBagConstraintsx14.gridy = 5;
        gridBagConstraintsx14.insets = new Insets(5,5,5,5); 
        container1.add(buttonForward, gridBagConstraintsx14);
                
        GridBagConstraints gridBagConstraintsx16 = new GridBagConstraints();
        gridBagConstraintsx16.gridx = 1;
        gridBagConstraintsx16.gridy = 6;
        gridBagConstraintsx16.insets = new Insets(5,5,5,5); 
        container1.add(buttonExit, gridBagConstraintsx16);
        
        
        buttonExit.addActionListener(this);
        buttonSave.addActionListener(this);
        buttonDelete.addActionListener(this);
        
   	}
   	
     	
   	public void actionPerformed (ActionEvent e)
   	{
   		
   		if (e.getSource() == buttonExit)
   		{			
   			System.exit(0);
   		}
   		else if (e.getActionCommand().equals("Exit"))
   		{	
   			System.exit(0);
   		}	
   		else if (e.getSource() == buttonSave)
   		{
   			this.details.addElement(this.textName.getText());
   			this.details.addElement(this.textAddress.getText());
   			this.details.addElement(this.textPhone.getText());
   			this.details.addElement(this.textMob.getText());
   		}
   		else if (e.getSource() == buttonDelete)
   		{
   			int j = 0;
            for (int i : theList.getSelectedIndices()) {
                details.removeElementAt(i - j);
                j += 1;
   		}
   		}	
   	}
   		
}

in an ideal world id like the data to be displayed as followed:

Frank, 86 Highlight Street, 0168168111, 07745729486.

so name, address, home number, moble number.

this is one of my first java projects so any help atal would be great, im starting to think that maybe a big text field would be better then a list because i also need the app to be able to import data from a file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.