Populate list dynamically in Codename one - java

I had some issues with a List concept in Codename one. I need a list of items populate dynamically at run time, but I don't know how doing it, so anyone helps me regarding this issue?

Depending on how your list is constructed... E.g. when you have a form with a container, which includes your list as labels.
Container c = new Container(BoxLayout.y());
//adding strings as labels
c.add("String1").add("String2");
Then you can later on add a String to this container
c.add("new String");
But the form won't update itself. So you'd have to
form.revalidate() OR form.animateLayout(150)
to be able to show the changes on the screen.

Related

Changing the contents of a JComboBox during runtime

Alright so I'm trying to create a combo box that will update it's contents during runtime except I have no clue how to do this without receiving a bunch of errors. Is there some sort of method that I can use in order to accomplish this? For example, I have a vector that might start out with the name in drawers 1 and 2 be hi and bye. Then during runtime the program will change drawer one and two to eggs, sausage and add a third drawer with the name being computer. How can I go about changing the name on a JComboBox during runtime?
You want to clear the combobox of all entries using removeAllItems(), then re-add the items from the Vector using addItem().
The data shown in the ComboBox actually lives in its model - some subclass of ComboBoxModel.
DefaultComboBoxModel has methods for adding and removing elements. If you want to completely replace the combo box's contents at runtime, the simplest way might be to just build a new model and call theComboBox.setModel(theNewModel) with it. Also see setSelectedItem() for setting the selection.

Adding/Deleting JPanels

I manage a list of plain object that I use to build and display JPanels showing information contained in those objects. One JPanel for each object in the list. Every seconds that list of object is updated with a new list. From this new list I'm checking if objects have been removed (not in the new list), or added, then update JPanels accordingly. At the moment what I'm doing is if at least one object has been removed or added from the list I delete all JPanels and add them all again. If not objects have been removed or added in the list I just update information on existing JPanels. I'm using GridBagLayout for that. I don't find this solution good enough and what I would like is removing/adding only JPanels corresponding to objects removed or added. Any suggestion on Layout to use or more optimized way of doing the work is welcomed, thanks.

how to create a Custom list model in Java

I'm trying to create list that shows contacts, each list item shows the name on a line, and the phone number on a second line, and maybe an image or icon. I was thinking of using two labels for that, but i can figure out how to use a custom list model to implement this.
My first attempt was to add a Panel object that contained the info i wanted in the list, then add it to an instance of the defualt list model, but that only turned up the class name in the list.
DefaultListModel Clistmodel = new DefaultListModel();//
Clistmodel.addElement(Contact);//Contact is an JPanel object
GroupList.setModel(Clistmodel);//GroupList is the List object
this didn't work out at all then i learnt that the default list model only knows how to render strings i think, so i have to create a custom list model, or a custom ListCellRenderer, i don't really know which will solve the problem.
Your question asks how to create a custom list model, however, that's not what you need (I don't think) as a DefaultListModel will work nicely for you. Rather you will need to work on the renderer. You need to create a non-GUI class to hold your information that each item will display, probably your Contact class, and then create a JList that holds this in its DefaultListModel.
The key for you will be to then create a custom list cell renderer to display the information on multiple lines -- perhaps a JTextArea, or a JPanel that holds two JLabels in a GridLayout. Please understand that the renderer does not display the actual underlying components, but something more akin to a stamped image of whatever components you're trying to display, so it will not have the full behaviors available to it as the actual component would. It will take work, but the writing a renderer section of the tutorial linked to by user714965 will show you how to do this.
Please give it a try, and then if you still are stuck, come on back with your code, your errors, and your questions, and we'll be better able to give you specific help.

Adding to a string array later with a method

I'm currently making an app for a forum, I want to add a favorites feature where there is a list of different sections of the forums people can add to their favorites list for quick access to them.
I'm having one slight problem though, I'm using a listview for the list of their favorites, so I need to add a new value to the listview when the user adds a new section to their favorites.
So really all I need to know, is how can I add to my string array for my list view, with a button? Or can I have all the options in the array and then decide witch ones show up according to what the user has chosen?
You could send an array of string with the current favourites to the list adapter. When more favourites are added, you can add them to the array and call
list.notfiyDataSetChanged();
This will update the list.
If you have a large enough array to hold all the possible options you can use setVisibleRowCount(int) to change how many are visible based on how many items are currently in the array.

How to Dynamically adding fields in JSF?

HI All,
I am desperately looking for the assistance on adding and removing the fields and rows using JSF. I am using ICE Faces for the rich UI look. The following is my problem:
I have to add one text box and two buttons (+) and (-) in a row.
When user clicks on the + button, one new row should be added with the above components.
When user clicks on the - button, the corresponding row should be removed from the display.
I am able to add the individual components like text box and remove it. But, here my challenging point is to add and remove as a row. How would I remove that specific row (instance) from the UI View Root.
I have tried several times, but till now I could not find a solution. Please suggest me the possible solutions.
Thanks in advance,
Krishna
Rather use a h:dataTable (or the IceFaces equivalent ice:dataTable) which is backed by a List<Data> contining objects which each represents the data of a single row. On Add just add a new Data item to the list. On Remove just remove the selected Data item from the list.
See also:
How to dynamically add new row to table? - detailed answer to similar question
Using Datatables - shows step by step how to use datatables in JSF 1.x.
CRUD datatable using #ViewScoped - simple example of JSF 2.0 CRUD

Categories