I'm binding a JComboBox to an observable List. I clear and add Objects form the observable list. This works fine and reflects the changes in the JComboBox correctly.
The problem is I can't select the content of the list using the mouse although the newly added items are correctly displayed in the combobox when expanded. The getSelectedItem() is always stuck on the first item in the list.
List<Object> sourceListObserver =
ObservableCollections.observableList(new ArrayList<Object>());
The binding is done using Netbeans GUI designer.
I have now also tried using DefaultComboBoxModel.
DefaultComboBoxModel model = new DefaultComboBoxModel();
wireSourceComboBox.setModel(model);
Using wireSourceComboBox.removeAllItems(); and wireSourceComboBox.addItem(qb);
Still the same behaviour after removing and adding objects to the combobox.
without a working example it's hard to prove, but you probably need to fire the event listeners to have the UI track your model correctly.
see AbstractListModel.fireContentsChanged
Related
I had quite a lot use for a multicolumn jcombobox, but have not yet found any nor managed to make my own. I have tried several approaches found in web, but they have not worked. Afterwards I read somewhere that those (old) does not work under current Java version.
I have managed to make my own so far, that the combobox has a table as dropdown list and I can select an item with mouse, but the goal is that when the user starts to type into the edit box, drop down list opens and cursor moves based on the text the user has written. It seems that the events from e.g. JTextField editor = (JTextField) comboBox.getEditor().getEditorComponent() does not work.
Has anybody managed to make a two column combobox or have any ideas, how I could get the event when the user starts to type.
You are looking for autocomplete functionality (as I understand the question): it's supported in SwingX - and quite easy to use.
It boils down to implemneting a custom ObjectToStringConverter and configure the comboBox with an autoCompleteDecorator using that converter. Something like:
/**
* A converter which expects an item of an array type and returns
* a string representation of its first value.
*/
public static class ArrayToStringConverter extends ObjectToStringConverter {
#Override
public String getPreferredStringForItem(Object item) {
if (!(item instanceof Object[])) return DEFAULT_IMPLEMENTATION.getPreferredStringForItem(item);
Object[] array = (Object[]) item;
return DEFAULT_IMPLEMENTATION.getPreferredStringForItem(array[0]);
}
}
// usage
// assuming an model with items being arrays
JComboBox combo = new JComboBox(arrayModel);
// the renderer supporting multiple columns, f.i. a table
combo.setCellRenderer(new TabularListRenderer());
AutoCompleteDecorator.decorate(combo, new ArrayToStringConverter());
A complete working example (including the renderer and showing how-to force the width of the popup to be larger than the combo itself) is TableAsListRenderer in my incubator section
BTW: the autocomplete functionality is a standalone module, accessible via maven or manually downloadable from the maven rep at java.net, you would want the swingx-autocomplete-1.6.4.jar (plus the corresponding doc/sources, if interested)
Has anybody managed to make a two column combobox or have any ideas, how I could get the event when the user starts to type.
you can to put JTable to the JComboBox, but by default you can to select only value from entire JTables row, not directly from JTables Cell (required additional workaround, not tried yet)
i still searching for this answer too
here's i try so far..
i create Jpopup and put the Jtable in there..
then i use jlabel not jcombobox,
when user click jlabel, then the popup(Jtable) will show in that jlabel location..
when user select value on jtable,
then the popup will dispose,
then the jlabel will show the result..
for your case, you can use jtextfield not jlabel
EDIT :
heres related question
enter link description here
could any one suggest me any method or any other way in java that i could deselect my jlist item when some event occurs?
i tried this but this does not seem to work
myJList.setSelectedIndex(-1);
myJList.ensureIndexIsVisible(-1);
Try myJList.clearSelection(); It clears the selection(s) on your JList object.
If multiple list items selected together, they can also be deselected via this method.
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 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.
I have a JComboBox binded to an observable list (result of a jpa query) in a java desktop application. It gets all the values from that list and displays them correctly, but I'm not able to select any particular value from the dropdown: that is, when I click on the widget the list of options gets displayed correctly, when I click on an item the list closes and the first value is shown independently from the selection of that particular item.
However, no exception is thrown.
This problem occurs if I have more than a model associated to the same entitymanager.
What's wrong with that?
I solved it using two separate entitymanagers for each model I have in the frame.