hey guys im not quicke sure this progrom wont send the string "result" to the JList, something im doing wrong? 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.sql.Array;
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, result;
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(400,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);
theList = new JList();
this.details = new DefaultListModel();
this.details.addElement("Fred Johnson, 85 Brick Road, 0123481244, 07813538241");
this.details.addElement("Barney Rubble, 1 Stone Henge, 02084579121, 07753985712");
this.details.addElement("Wilma Jetson, 1 Space Street, 01026583751, 07717496739");
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();
inputResults();
}
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 inputResults ()
{
result = textName.getText()", "+textAddress.getText()", "+textPhone.getText()", "+textMob.getText();
}
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.result);
}
else if (e.getSource() == buttonDelete)
{
int j = 0;
for (int i : theList.getSelectedIndices()) {
details.removeElementAt(i - j);
j += 1;
}
}
}
}