Implementing a JList from custom objects efficiently? - java

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

Related

Select from a UI list and perform an action

we're building a small chat app for an assignement in our university. I have a question regarding how I can implement something.
This is our ui. The big white part is a jTabbedPane where conversations the user is participating in will appear. The two small ones are where active groups and active users will appear.
I found out that I can populate a jcombobox from a linkedlist using .toArray. I don't know what ui element to use, in order to display the list elements one under the other, and being "selectable" (only one at a time). The concept is that the user will select a group and press "Join", to, well, join.
This is what I have in my as to how it will look in the end.
Any pointers and advice in general would be greatly appreciated.
It looks like you're wanting to use either a JTable or a JList -- one with a custom renderer, a renderer that displays both the group name and its "status"(?).
If a JTable, then your key job is to create a TableModel that will accept your data well, either by using the DefaultTableModel (the easiest way to do this), or by creating your own model derived from the AbstractTableModel (a little more difficult, but more flexible).
For a more detailed answer, consider providing pertinent code, preferably as a minimal example program or MCVE.

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 I can get an array with 2 items from a selected item in a JComboBox?

I have an ArrayList<ArrayList<Object>> Suppliers. Each ArrayList<Object> has 2 elements, an int (an id and the id may not be sequential with each other.) and a string (a name).
Now in a JComboBox, I create a DefaultComboBoxModel, containing the main ArrayList 'Suppliers'.
jcbSuppliers.setModel (new DefaultComboBoxModel (suppliersdata.Suppliers.toArray ()));
The JComboBox shows me each element such as [1, Local Supplier], [2, External Supplier], [4, Other Supplier].
Capture 1
In NetBeans debug, i put a Watch on jcbSuppliers. The variables window shows me that each element of jcbSuppliers is an ArrayList with 2 Objects.
Capture 2
Now my question is:
How I can get these 2 items or the array of these 2 objects, but of the selected object or item?
I would appreciate the help. Thank you very much.
First I do have to give kudos to Flextra and his comment as Spring can help you decouple your code, making it much easier to enhance and debug.
Next I suggest that you re-think your current model set up. Rather than begin with nested ArrayLists, I suggest that you create a custom class to hold the two pieces of data each supplier will need.
Then create a non-nested ArrayList of this custom class.
Then you can tell the JComboBox how to display an object of your either by giving it a toString() method that displays the information as you'd like it,
or give the JComboBox a custom cell renderer that displays the information as you'd like it.
Then when the combo box is selected, if you get the selectedItem, it will be an object of your class, and it will be easy to extract the two pertinent pieces of information.

Codename One List Containing Lists

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.

Fixing the maximum size of a JList and changing the look of empty cells

I'm working on UI stuff with a JList that can contain at most 8 items. Logically I have prevented the application from adding to the JList once it has reached it's limit, however, I was wondering if there was a way to perhaps explicitly set a limit on the JList itself. Which would help with the next thing I want to do, which is to repaint empty cells (up to 8 cells at most) to indicate that they are empty slots that can be filled. I'm trying to find a way to do this (perhaps by extending ListUI) which does not involve adding placeholder elements into the JList to represent empty spaces.
I think the most simple way is to implement a list model (using DefaultListModel or AbstractListModel) My idea is that the model will always have 8 items. All of them with a empty message.
If you add a new item to the model you can replace the empty text with a specific text for that item. The model will keep the track about how many items are added to the model. If you try to add more that MAX_ITEMS you can thow an exception or something like that.
Implement DefaultListModel is really easy and you have a lot of samples on Internet.
You can try the approach shown here, which limits growth to N entries, with scrolling enabled thereafter. A ListCellRenderer, shown here, may be used to alter the item's appearance.

Categories

Resources