I have a List box in my UI design, however, I can't access the .getElement() method that many people seem to be accessing when implementing a get method.
Is there any reason for this and is there a way to add values to this list box?
I have a List box in my UI design,
I assume you mean JList when using Swing. "List" is an AWT component. Be specific when you ask a question to avoid confusion.
Is there any reason for this and is there a way to add values to this list box?
You add items to the DefaultListModel, not the JList.
Read the section from the Swing tutorial on How to Use Lists for more information and working examples.
Related
I am working to create a custom layout for an application, as I am not able to implement the functionality using existing layouts like CardLayout.
I need to store various Component instances in a collection. As, I see in CardLayout source code, they've used Vector to hold Card instances.
By going through many answers on StackOverflow, I have learned, we should use ArrayList over Vector. But, which one should we prefer in the case of GUI related applications like holding Component instances in a LayoutManager.
Note: anywhere I refer to a List in this question, I am talking about com.codename1.ui.List.
Can I use Codename One's GUI Designer to put a List in a List and then populate each sub-list with its own data?
I can define a Container in the GUI Designer and put a List in it (I'll call it InnerList) then set the Container as the Renderer for another List (I'll call this one OuterList.) What I have been unable to do is to then set the data for the InnerList in a particular cell of OuterList. When I modify OuterList's ListItems in the GUI, InnerList doesn't even appear in the list of keys, and I have not found a way to populate InnerList from code either.
To work around this problem, I have had to define two user classes implementing ListCellRenderer (and extending Container) and, in code, set them as the renderers for OuterList and InnerList, respectively. In the GUI Designer, there is only OuterList with no apparent Renderer. This works, but it doesn't really fit in with the way the rest of the GUI is defined and it adds confusion.
No.
This wouldn't make any sense. Codename One list renderer's are "rubber stamps" which means they have no state and so you will not be able to vary the size of the elements or access individual components within them. What you need to do is use a Component->Container hierarchy to achieve the functionality you are seeking.
I need to implement a JList to display the contents of an Array-List. The ArrayList contains "custom" objects, from which I only want to display the title.
A "custom object" can have
- title
- user
etc.
What would be the most efficient way to do this as opposed to what I'm doing now (going through the entire array and building another array of strings with title[i] = customObject.getTitle())?
Thanks to anyone who will take the time to answer...
there no restrictions, bug in the code, for better help sooner post an SSCCE, demonstraded your issue(s)
(don't reinvent the wheel) create a DefaultListModel that hold value for JList
all updates to the ListModel must be done on Event Dispatch Thread
for most complex output to the GUI or array is based on java.util.List, Map, HashMap, then to use JTable with one column based on AbstractTableModel, because JList has implemented reduced methods or features in compare with JTable
I implemented my own ListCellRenderer for my chat app. I use a JList to list all users. The cell renderer consists mainly of an icon that displays if a particular user is currently on- or offline and his/her name. The list is controlled by a DefaultListModel which I use to provide the JList with the necessary information.
But when the list model does change its state (e.g. a user goes offline), the list cell renderer seems not to be invoked?
Somebody any idea how to solve this problem? Tried to call updateUI() on the JList instance, but did not helped.
Many thanks in advance!
The cell renderer probably works fine. What is not working is the ListModel. The DefaultListModel does not detect changes to the internal state of the model objects. You need to call fireContentsChanged on the list model. Probably you need to add listeners to your model objects and maybe you even have to extend the DefaultListModel; as I don't see the code of it I don't know how yours look.
You should not just call a random method with a name that sounds good (updateUI does something very different).
I understand how to make a multiple-select list box using JLists but I want to add JCheckBoxes to the list and make it dropdown like. The best visual representation I have found online is dropdown-check-list.
What would be the best way to accomplish the above?
I was thinking of a TableList. Any suggestions?
If you are using JList, then its as simple as changing the ListCellRenderer to return a JCheckbox component.
EDIT:
For JCombobox, you can use combobox.setRenderer(myListRenderer);
This code snippet may help you.
The basic idea is to handle actionPerformed or mouseClick events by yourself and keep states of the corresponding items (checked/unchecked) in your own data structure. You'll be able to use that data structure for rendering checkboxes in a dropdown