Java: How to update custom ListCellRenderer? - java

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

Related

Creating custom JList that runs method other than toString for Objects

In the game I'm trying to create, I have a class which displays the inventory for a character in a JList. I understand that populating a JList with Objects will cause the toString method for that object to run when the JList is actually displayed. I want to create a custom JList that does something other than run the toString method on the Objects populating it. I understand that I can overwrite the toString method on Objects, but I need the toString method to be different than what's displayed in the inventory. For some reason, I'm unable to view classes such as Object or JList (I can still view my own), otherwise I'd look through the code myself. (TL;DR - I need to overwrite the method in JList that runs toString on the objects populating it)
Side note: only a specific type of Object will be put into the JList, so I know what I intend to run instead of toString, and the custom JList will only be used in 1 class.
The correct way to do this is with ListModel. For details, read the official tutorial on How to Use Lists. Pay close attention to the section on Creating a ListModel.
Maybe simplest way is to make a sub class from JList - a new class extends JList - and add the method(s) you need and use it instead of JList .
I managed to find the answer for myself. A custom class must be written extending JLabel implementing ListCellRenderer<>. In this, you can specify exactly what goes into the text component of each cell in the JList.

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.

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.

Changing a JComboBox model unsing a ListModel. Does it have hidden consecuences?

I use both JList and JComboBox in different places. The content of both change dynamically.
Once a comboBox is created you cant just say comboBox.setModel(String[]), you have to create a new model and then set it to the comboBox.
Same happens with the JList.
Rather than creating my own Jlist and ComboBox just to add a new method called .setNewModel(String[]) i created a static method in my "utility" class that receives a String[] and returns a ListModel.
So i can do this:
someComboBox.setModel((ComboBoxModel)UtilityClass.convetToListModel(aStringArray));
I use the same for the JList.
someList.setModel(UtilityClass.convetToListModel(anotherStringArray));
my question is:
Could the casting of the listModel as a ComboBoxModel have some unexpected consequences?
If so, is there anyway to change the entire content of a comboBox without having to transform the ArrayString into a Model?
here is the code of the method:
public static ListModel convertToListModel(String[] nList)
{
return (new JComboBox(nList).getModel());
}
The program compiles and runs fine, but casting always generates doubts in me, specially complex objects.
Yes i know i can extend JComboBox and JList to add a method that does the job but its a lot of extra work. Why the ComboBox and Jlist don't have a update or modify Model than accept a simple array of Strings?
How is
someComboBox.setModel((ComboBoxModel)UtilityClass.convetToListModel(aStringArray));
in any way easier to write/simpler/whatever than
someComboBox.setModel(new DefaultComboBoxModel(aStringArray))
all you added is white noise in the form of the Utility method. Plus
the implementation of that method is simply ... crazy: you create a JComboBox just for the sake of accessing the model that's internally created by that combo ...
you have to exploit implementation to type-cast for usage in a real combo ...
Don't do such wasteful/unnecessary stuff, don't even think of going any detours when there's a simple straightforward manner to reach the same goal
If the contents of the list/combobox need to change dynamically, then you should manage the model itself directly. You shouldn't create a new model each time and replace the old one. The whole point of having a model is that you can update the data it contains.
Simply create your own DefaultListModel or DefaultComboBoxModel and pass it into the JList/JComboBox. Then use the model's add/remove methods as needed to update the contents when it changes.
private DefaultComboBoxModel model = new DefaultComboBoxModel();
private JComboBox combo = new JComboBox(model);
...
model.addElement(somethingForMyList);
...
model.removeAllElements();
...
model.removeElement(elementToRemove);
Usually I would prefer to implement a new class that is inherited from DefaultComboBoxModel (therefore it's also a ListModel as well as a ComboBoxModel). This new class would be enriched with methods to update the model as any possible situation demands. In the update methods you would call fireContentsChanged to tell the enclosing component that the contents have changed and the component should redraw everything.
Hope it helps.

Java Dropdown Checklist

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

Categories

Resources