Results 1 to 3 of 3
  1. #1
    Ekul is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default string wont send to JList

    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;
    }
    }
    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you want to shrink that code to something much smaller but that compiles, shows your problem and is easy for us to review quickly?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Not only that you already have a related posting on this topic which you have ignored and not bothered to thank people for their help.

Similar Threads

  1. Replies: 5
    Last Post: 11-06-2009, 06:24 PM
  2. Why wont my object render properly
    By toecutter in forum AWT / Swing
    Replies: 3
    Last Post: 10-22-2009, 02:43 PM
  3. why wont it compile
    By bje98f in forum Advanced Java
    Replies: 1
    Last Post: 04-23-2009, 11:55 PM
  4. Replies: 6
    Last Post: 01-19-2009, 09:28 PM
  5. save will work but load wont?!?!
    By Sticks_ll in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 05:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •