×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Feb 2012
    Posts
    60

    Multiple Labels in LWUIT

    Hi, i am using LWUIT list in which i displays 36 items. now i want to display the label againts each item.

    i am not sure how to do it. i am using array of labels but not work.

    here is some code


    DefaultListModel simpleListModel = new DefaultListModel(items);
    menu=new List(simpleListModel);
    DefaultListCellRenderer dlcr=new DefaultListCellRenderer();
    dlcr.getListFocusComponent(menu);
    menu.setRenderer(dlcr);
    menu.setRenderingPrototype("WWWW");
    menu.getStyle().setBgTransparency(65);
    menu.getStyle().setBorder(Border.createLineBorder(100));
    menu.setItemGap(1);
    addComponent(menu);

    menu is List
    this code displays List . how to add labels to each item.

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Multiple Labels in LWUIT

    Hi rana.anees,

    Your code looks fine, except that you need to specify a layout when adding the List component to top level container, i.e your LWUIT Form. I created a List of 4 items which I could successfully display them on the screen by using your code as follows:
    Code:
    import com.sun.lwuit.Button;
    import com.sun.lwuit.Display;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.List;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import com.sun.lwuit.layouts.BorderLayout;
    import com.sun.lwuit.list.DefaultListCellRenderer;
    import com.sun.lwuit.list.DefaultListModel;
    import com.sun.lwuit.plaf.Border;
    
     public class LWUITListMIDlet extends javax.microedition.midlet.MIDlet implements ActionListener {
    	 Button exitButton;
    	 Form mainform;
       
    	public void startApp() {
    		//init the LWUIT Display
    		Display.init(this);
    		mainform = new Form("LWUIT List");
    		exitButton = new Button("Exit");     
    		mainform.setLayout( new BorderLayout());
    		
    		//A list of items
    		String[] items = { "Red", "Blue", "Green", "Yellow" };
    
    		// Your code starts here
    		DefaultListModel simpleListModel = new DefaultListModel(items);
    		List menu = new List(simpleListModel);
    		DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
    		dlcr.getListFocusComponent(menu);
    		menu.setRenderer(dlcr);
    		menu.setRenderingPrototype("WWWW");
    		menu.getStyle().setBgTransparency(65);
    		menu.getStyle().setBorder(Border.createLineBorder(100));
    		menu.setItemGap(1);   
    		//Adding the list with a centered border layout.
    		mainform.addComponent(BorderLayout.CENTER, menu);
    		//Your code ends here
    		
    		
    		exitButton.addActionListener(this);
    		mainform.addComponent(BorderLayout.SOUTH,exitButton);
    		mainform.show();
       }
     
       public void pauseApp() {
    	   //To-Do
       }
     
       public void destroyApp(boolean unconditional) {
    	   //To-Do
       }
    
        public void actionPerformed(ActionEvent ae) {
            notifyDestroyed();
        }
        
    }

  3. #3
    Regular Contributor
    Join Date
    Feb 2012
    Posts
    60

    Re: Multiple Labels in LWUIT

    I think you misunderstand a question.I have created a list i want to add a label against each to display a string how can i do that. Kindly some me some code of displaying labels against each item in list. thanks for reply.

  4. #4
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Multiple Labels in LWUIT

    Apologies for the misunderstanding. In that case, why don't you just create a container and add each label (which is a component) to your container? If you set the Layout of the container to BoxLayout.Y_AXIS, then each label will be placed one on top of the other.
    This is a sample code:
    Code:
    import com.sun.lwuit.Button;
    import com.sun.lwuit.Container;
    import com.sun.lwuit.Display;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.Label;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import com.sun.lwuit.layouts.BorderLayout;
    import com.sun.lwuit.layouts.BoxLayout;
    
     public class LWUITListMIDlet extends javax.microedition.midlet.MIDlet implements ActionListener {
    	 Button exitButton;
    	 Form mainForm;
       
    	public void startApp() {
    		//init the LWUIT Display
    		Display.init(this);
    		mainForm = new Form("LWUIT List");
    		exitButton = new Button("Exit");     
    		mainForm.setLayout( new BorderLayout());
    		
    		//Creates a container for all the Labels
    		Container list = new Container();
    		//The list of Labels
    		Label item1 = new Label("Item 1");
    		Label item2 = new Label("Item 2");
    		Label item3 = new Label("Item 3");
    		//Adds all the Labels to the container
    		list.addComponent(item1);
    		list.addComponent(item2);
    		list.addComponent(item3);
    		//Sets the container's layout to BoxLayout 
    		//This enables the list of Labels to be placed one
    		//above the other
    		BoxLayout boxlayout = new BoxLayout(BoxLayout.Y_AXIS);
    		list.setLayout(boxlayout);
    
    		//Adding the list with a centered border layout.
    		mainForm.addComponent(BorderLayout.CENTER, list);
    		
    		//An additional Exit Button
    		exitButton.addActionListener(this);
    		mainForm.addComponent(BorderLayout.SOUTH,exitButton);
    		mainForm.show();
       }
     
       public void pauseApp() {
    	   //To-Do
       }
     
       public void destroyApp(boolean unconditional) {
    	   //To-Do
       }
    
       public void actionPerformed(ActionEvent ae) {
            notifyDestroyed();
       }  
    }

Similar Threads

  1. How to Display multiple lines labels with Dynamic data
    By yanamaddinaveen in forum Symbian User Interface
    Replies: 4
    Last Post: 2012-01-24, 13:19
  2. Scrolling Multiple labels in a Container
    By marvik_34 in forum Symbian
    Replies: 1
    Last Post: 2011-03-16, 08:05
  3. LWUIT multiple selection list
    By senthil_25 in forum Mobile Java General
    Replies: 2
    Last Post: 2010-11-12, 10:35
  4. LWUIT Labels Font size problem
    By superchacho in forum Mobile Java General
    Replies: 4
    Last Post: 2010-06-16, 09:00
  5. displaying labels with multiple lines and fonts in a List
    By rien in forum Mobile Java General
    Replies: 6
    Last Post: 2007-09-13, 00:47

Posting Permissions

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