Codename One List Containing Lists - java

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.

Related

Can't add to List

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.

How can I make a JList interact with my database?

I have a list of items that are pulled from a database, it combines the various fields with a rs.getString method to create a longer string of items, this is done in an action button method.
I would like to be able to click on an item in this list and have one of the fields display as text in a textbox, so this needs to be done through the list selection event method where I instruct the program to set the text to my desired value.
My problem is, I am not sure of the logic to follow in order to specify how to retrieve that fields information that will be corresponding to the item that is selected in the list, can you give me any ideas?
Rather the combining the fields into a single String, create a POJO (Plain Old Java Object), which provides getters (and possible setters) for the fields you want and these objects to the ListModel
Use a ListCellRenderer to customise the way which the JList renders the POJO the way you want to. See Writing a Custom Cell Renderer for more details.
When the user selects an item from the list, use JList#getSelectedValue and cast to the same class as your POJO. You can now use the POJO's getters to extract the properties you want to display.
The idea is to generate a self contained unit of work, which, based on what you want to do, you can customise how the object is displayed.
This concept is a corner stone to the separation of data (model) and the UI (view) behind the Model-View-Controller paradigm and OOP generally...

Which one to use Vector or List (ArrayList) in creating custom layout for swing?

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.

SWT ComboBoxCellEditor editable

I have a TableViewer where the values in one column should typically come from a dynamic list.
I'm currently using org.eclipse.jface.viewers.ComboBoxCellEditor , which is actually a Select-List: it stores the index of the selected value. If I change the underlying list (calling setItems(String[]), it's clumsy to keep the previous selected value... (specially if it's not included in the list anymore!) What I'd wish is actually a cell editor that stores, not the index from the list, but the string (perhaps letting the user edit it freely, perhaps not), where the list is just used as a suggestion at input time - like a "combobox" was supposed to work in the good old days... Is this possible?
I would suggest you to have your CellEditor to mimic the behavior that you are looking for. Extend ComboBoxViewerCellEditor and override doGetValue() method. Add modify listener on Combo control and also filter (which filters list items based on input text) to comboviewer.
You should look at :
org.eclipse.wst.xml.ui.internal.properties.StringComboBoxCellEditor This class comes from WTP project; It's an extended ComboBoxCellEditor that selects and returns Strings.
codemirror.eclipse.ui.xquery.viewers.StringComboBoxCellEditor It's the copy/paste of WTP StringComboBoxCellEditor; it adds the capability to add the item in the combo when it is not found.

Java: How to update custom ListCellRenderer?

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).

Categories

Resources