I have created 3 files: SmsInbox, SmsGetData and SmsMsg.

I don't know if I set this up right. I want Phonenumber, message and time to be stored in the Jlist and when I double click it It will print out the phonenumber, message and time.

How do I print it out?

I would appreciate any assistance on how to get this to work. I comment the code in the mouse listener.

another thing, Does this need to be a vector. How do convert this to a vector object?

import java.awt.BorderLayout;  

import java.awt.event.ActionEvent;  

import java.awt.event.ActionListener;  

import java.awt.event.MouseAdapter;  

import java.awt.event.MouseEvent;  

import java.awt.event.MouseListener;  

    

import javax.swing.DefaultListModel;  

import javax.swing.JButton;  

import javax.swing.JFrame;  

import javax.swing.JList;  

   

import javax.swing.JPanel;  

import javax.swing.JScrollPane;  

import javax.swing.ListSelectionModel;  

    
    
    

public class SmsInbox extends JPanel   

{  

    

   JList list;  

    

   DefaultListModel model;  

   int counter = 0;  

    

   private SmsInbox()  

   {  

     setLayout(new BorderLayout());  

     model = new DefaultListModel();  

     list = new JList(model);  

     list.setSelectedIndex(0);  

     list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  

 

     JScrollPane pane = new JScrollPane(list);  

     JButton refreshButton = new JButton("Refresh");  

     JButton deleteButton  = new JButton("Delete Message");  

   

   

     refreshButton.addActionListener(new ActionListener()   

     {  

       public void actionPerformed(ActionEvent e)  

       {  

              

           SmsGetData data = SmsGetData.getSmsGetData();    

           data.addData();  

       }  

     });  

    

     deleteButton.addActionListener(new ActionListener()   

     {  

       public void actionPerformed(ActionEvent e)   

       {  

           model.removeElement(list.getSelectedValue());  

       }  

     });  

    

     MouseListener mouseListener = new MouseAdapter() {  

      public void mouseClicked(MouseEvent e) {  

          if (e.getClickCount() == 2) {  

              int index = list.locationToIndex(e.getPoint());  

              System.out.println("Double clicked on Item " + index);  

                 

           //   SmsMsg sel = new SmsMsg();  

    

          //    sel = list.getModel().getElementAt(list.locationToIndex(e.getPoint()));  

          //    System.out.println("Phone Number: " + sel.getPhoneNbr());  

          //    System.out.println("SMS message: " + sel.getMsg());  

          //    System.out.println("SMS message time: " + sel.getTime());  

                 

           }  

      }  

     };  

     list.addMouseListener(mouseListener);  

    

    

     add(pane, BorderLayout.NORTH);  

     add(refreshButton, BorderLayout.WEST);  

     add(deleteButton, BorderLayout.EAST);  

    

   }  

    

   //public void addMsg(String element)  

   public void addMsg(String phoneNbr,String msg, String time)  

   {  

       model.addElement(new SmsMsg(phoneNbr, msg, time));  

       //model.addElement(element);  

   }  

      

   public static SmsInbox getSmsInbox()  

   {  

     if (ref == null)  

     {  

         // it's ok, we can call this constructor  

         ref = new SmsInbox();  

     }  

     return ref;  

   }  

      

   private static SmsInbox ref;  

      

   public static void main(String s[])  

   {  

     JFrame frame = new JFrame("SMS Inbox");  

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

     frame.setContentPane(getSmsInbox());  

     frame.setSize(260, 200);  

     frame.setVisible(true);  

   }  

 }  

 ++++++++++++++++++++++++++++++++++++++++++++++++++  

    

 public class SmsGetData   

 {  

     SmsInbox msg = SmsInbox.getSmsInbox();  //call singleton  

    

     private SmsGetData()  

     {  

            

     }  

        

     public static SmsGetData getSmsGetData()  

     {  

         if (ref == null)  

         {  

            // it's ok, we can call this constructor  

             ref = new SmsGetData();  

         }  

         return ref;  

     }  

          

       private static SmsGetData ref;  

          

     public void addData()  

     {  
            

         msg.addMsg("623 123-1231","message 1","12:01 PM");  

         msg.addMsg("623 123-1232","message 2","12:02 PM");  

         msg.addMsg("623 123-1233","message 3","12:03 PM");  

         msg.addMsg("623 123-1234","message 4","12:04 PM");  

         msg.addMsg("623 123-1235","message 5","12:05 PM");  

     }  
       

 }  

 ===========================================================  

 public class SmsMsg  

 {  

    

    private String phoneNbr;  

    private String msg;  

    private String time;  

    

    public SmsMsg(String phoneNbr, String msg, String time)  

    {  

        

      this.phoneNbr = phoneNbr;  

      this.msg      = msg;  

      this.time     = time;  

       

    }  

    

    public String getPhoneNbr()  

    {  

       return phoneNbr;  

    }  

    

    public String getMsg()  

    {  

       return msg;  

    }  

      

    public String getTime()  
    {  
       return time;  
    }  

    

 }

> I only want to display a list of incoming string phonenumber
JList will display the toString() result of objects in the model, so you can override toString() in your SmsMsg class to display the phone number.

As for double-clicking, you can retrieve the currently selected item with list.getSelectedValue() and cast that to SmsMsg to do anything you want with the object (ie print data from it)

I already told you above - override toString() in your SmsMsg class to display the phone number.

Yes. The JList will display the string representation of the objects by calling their toString() method. If you customize that, you can display any string that you wish.

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.