ComboBoxModel - removeItem method for fireIntervalRemoved throws exception - java

I've created a ComboBoxModel class which extends AbstractListModel. I can add item to the combobox, but when I try to remove, I get an exception
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: null source
at line
this.fireIntervalRemoved(selectedItem, itemIndex, itemIndex);
public class TComboBoxModel extends AbstractListModel implements ComboBoxModel {
private int itemIndex;
private Object selectedItem = null;
private ArrayList<Object> itemList;
public TComboBoxModel() {
itemList = new ArrayList<>();
}
public void addItem(String item) {
this.itemList.add(item);
this.fireIntervalAdded(item, itemIndex, itemIndex);
}
public void removeItem() {
if (itemIndex >= 0 && itemIndex < getSize()) {
this.itemList.remove(itemIndex);
this.fireIntervalRemoved(selectedItem, itemIndex, itemIndex);
}
}
#Override
public void setSelectedItem(Object anObject) {
if ((selectedItem != null && !selectedItem.equals(anObject)) || selectedItem == null && anObject != null) {
this.selectedItem = anObject;
this.fireContentsChanged(anObject, -1, -1);
}
}
#Override
public Object getSelectedItem() {
return selectedItem;
}
#Override
public int getSize() {
return itemList.size();
}
#Override
public Object getElementAt(int index) {
return itemList.get(index).toString();
}
public int getItemIndex() {
return itemIndex;
}
public void increaseItemIndex() {
itemIndex++;
}
public void decreaseItemIndex() {
itemIndex--;
}
}

Pass this to the fire* methods in the model. The event source is the model, not the item.
From the documentation:
source - the ListModel that changed, typically "this"

You should probably change it to say:
if (selectedItem != null) {
fireIntervalRemoved(this, itemIndex, itemIndex);
}
Since you can't remove an item unless you know which one to remove by having a selected item.
You are going to have to be setting the itemIndex variable appropriately too.
public void setSelectedItem(Object anObject) {
if ((selectedItem != null && !selectedItem.equals(anObject)) || selectedItem == null && anObject != null) {
this.selectedItem = anObject;
this.fireContentsChanged(anObject, -1, -1);
itemIndex = ... index in itemList where anObject is located (or -1 if not found) ...
}
}
Thanks to #kiheru for pointing out the problem with the 1st argument.

Related

How to get a ComboBox to use an Object's toString method instead of setting it as a graphic? [duplicate]

I have a combobox which shows list of User objects. I have coded a custom cell factory for the combobox:
#FXML ComboBox<User> cmbUserIds;
cmbUserIds.setCellFactory(new Callback<ListView<User>,ListCell<User>>(){
#Override
public ListCell<User> call(ListView<User> l){
return new ListCell<User>(){
#Override
protected void updateItem(Useritem, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getId()+" "+item.getName());
}
}
} ;
}
});
ListView is showing a string(id+name), but when I select an item from listview, Combobox is showing toString() method return value i.e address of object.
I can't override toString() method, because the User domain object should be same as the one at server.
How to display id in combobox? Please suggest
EDIT1
I tried below code. Now combo box shows id when I select a value from the listview.
cmbUserIds.setConverter(new StringConverter<User>() {
#Override
public String toString(User user) {
if (user== null){
return null;
} else {
return user.getId();
}
}
#Override
public User fromString(String id) {
return null;
}
});
The selected value in combo box is cleared when control focus is lost. How to fix this?
EDIT2:
#FXML AnchorPane root;
#FXML ComboBox<UserDTO> cmbUsers;
List<UserDTO> users;
public class GateInController implements Initializable {
#Override
public void initialize(URL location, ResourceBundle resources) {
users = UserService.getListOfUsers();
cmbUsers.setItems(FXCollections.observableList(users));
cmbUsers.getSelectionModel().selectFirst();
// list of values showed in combo box drop down
cmbUsers.setCellFactory(new Callback<ListView<UserDTO>,ListCell<UserDTO>>(){
#Override
public ListCell<UserDTO> call(ListView<UserDTO> l){
return new ListCell<UserDTO>(){
#Override
protected void updateItem(UserDTO item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getUserId()+" "+item.getUserNm());
}
}
} ;
}
});
//selected value showed in combo box
cmbUsers.setConverter(new StringConverter<UserDTO>() {
#Override
public String toString(UserDTO user) {
if (user == null){
return null;
} else {
return user.getUserId();
}
}
#Override
public UserDTO fromString(String userId) {
return null;
}
});
}
}
Just create and set a CallBack like follows:
#FXML ComboBox<User> cmbUserIds;
Callback<ListView<User>, ListCell<User>> cellFactory = new Callback<ListView<User>, ListCell<User>>() {
#Override
public ListCell<User> call(ListView<User> l) {
return new ListCell<User>() {
#Override
protected void updateItem(User item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getId() + " " + item.getName());
}
}
} ;
}
}
// Just set the button cell here:
cmbUserIds.setButtonCell(cellFactory.call(null));
cmbUserIds.setCellFactory(cellFactory);
You need to provide a functional fromString() Method within the Converter!
I had the same problem as you have and as I implemented the fromString() with working code, the ComboBox behaves as expected.
This class provides a few of my objects, for dev-test purposes:
public class DevCatProvider {
public static final CategoryObject c1;
public static final CategoryObject c2;
public static final CategoryObject c3;
static {
// Init objects
}
public static CategoryObject getCatForName(final String name) {
switch (name) {
case "Kategorie 1":
return c1;
case "Cat 2":
return c2;
case "Steuer":
return c3;
default:
return c1;
}
}
}
The converter object:
public class CategoryChooserConverter<T> extends StringConverter<CategoryObject> {
#Override
public CategoryObject fromString(final String catName) {
//This is the important code!
return Dev_CatProvider.getCatForName(catName);
}
#Override
public String toString(final CategoryObject categoryObject) {
if (categoryObject == null) {
return null;
}
return categoryObject.getName();
}
}

ComboBoxModel as wrapper for ListModel

I want a javax.swing.ListModel be shared among multiple javax.swing.ComboBoxModels.
With the following code , when I select an item from the JComboBox,in a running program through the view, the JComboBox always shows the first item as selected.
public class DelegatedComboBoxModel<T> extends AbstractListModel<T> implements
ComboBoxModel<T> {
protected ListModel<T> listModel;
protected Object selectedObject;
ListDataListener listDataChangeDelegater = new ListDataListener() {
#Override
public void intervalRemoved(ListDataEvent e) {
fireIntervalRemoved(DelegatedComboBoxModel.this, e.getIndex0(),
e.getIndex1());
}
#Override
public void intervalAdded(ListDataEvent e) {
fireIntervalAdded(DelegatedComboBoxModel.this, e.getIndex0(),
e.getIndex1());
}
#Override
public void contentsChanged(ListDataEvent e) {
fireContentsChanged(DelegatedComboBoxModel.this, e.getIndex0(),
e.getIndex1());
}
};
public DelegatedComboBoxModel(ListModel<T> listModel) {
// DefaultComboBoxModel<E>
this.listModel = listModel;
if (listModel.getSize() > 0) {
selectedObject = listModel.getElementAt(0);
}
listModel.addListDataListener(listDataChangeDelegater);
}
#Override
public T getElementAt(int index) {
if (index >= 0 && index < listModel.getSize())
return listModel.getElementAt(index);
else
return null;
}
#Override
public int getSize() {
return listModel.getSize();
}
#Override
public void setSelectedItem(Object anObject) {
if ((selectedObject != null && !selectedObject.equals(anObject))
|| selectedObject == null && anObject != null) {
selectedObject = anObject;
fireContentsChanged(this, -1, -1);
}
}
#Override
public Object getSelectedItem() {
return selectedObject;
}
}
I cannot figure out what went wrong with the above code.
How to fix the code ?
(Limitation : Should not use or subclass DefaultComboBoxModel or use any third-party library.)

Creating an add method that doesn't add to the list if its a duplicate?

I'm trying to override a method that adds a string to a list. But i want the method to not add the string if the string is a duplicate in the list. If it isn't a duplicate than add if not do nothing. Here is my code. I'm just confused why it is not working.
public class Hw6 <T extends Comparable<? super T>> implements SortedListInterface<T>
{
private ListInterface<T> list;
public Hw6()
{
list = new LList<T>();
}
#Override
public boolean add(T newEntry) {
boolean results = false;
if(!contains(newEntry))
{
list.add(newEntry);
results = true;
}
return results;
}
public boolean addPrivate(T newEntry)
{
int newPosition = Math.abs(getPosition(newEntry));
return list.add(newPosition, newEntry);
}
#Override
public boolean remove(T anEntry) {
boolean result = false;
int position = getPosition(anEntry);
if (position > 0)
{
list.remove(position);
result = true;
}
return result;
}
#Override
public int getPosition(T anEntry) {
int position = 1;
int length = list.getLength();
while((position <= length) && (anEntry.compareTo(list.getEntry(position)) > 0))
{
position++;
}
if ((position > length) || (anEntry.compareTo(list.getEntry(position)) != 0))
{
position = -position;
}
return position;
}
#Override
public T getEntry(int givenPosition) {
return list.getEntry(givenPosition);
}
#Override
public boolean contains(T anEntry) {
boolean found = false;
for (int index = 0; !found && (index < getLength()); index++)
{
if (anEntry.equals(list.getEntry(index)))
found = true;
}
return found;
}
#Override
public int getLength() {
// TODO Auto-generated method stub
return list.getLength();
}
#Override
public boolean isEmpty() {
if(getLength() == 0)
return true;
else
return false;
}
#Override
public boolean isFull() {
return false;
}
public static void main(String args[])
{
LList<String> list = new LList<String>();
list.add("brad");
list.add("nick");
list.add("brad");
for(int i = 1; i <= list.getLength(); i++)
{
System.out.println(list.getEntry(i));
}
}
}
here is my output. i don't want it to add brad because its a duplicate
brad
nick
brad
It is because in your test you are creating a LList object but you should be creating a Hw6 object.

Item displays get key added but dont show in the inventory

When adding the key it displays key added but then when I type inventory it says backpack empty, also when I go to use the item it wont let me, as its not usable.
public class Get extends AbstractCommand {
public Get (String name, String description){
super (name, description);
}
#Override
public void execute (Game game, String string) {
Item temp = game.getCurrentRoom().getItem(string);
UserInterface ui = game.getUI();
if(temp == null) ui.println("Item added\n");
else if (!(temp instanceof Moveable)) ui.println("item can not be moved");
else if (!game.getBackpack().add(temp)) ui.println("item can not be moved");
else game.getCurrentRoom().removeItem(temp);
}
}
#Override
public void execute(Game game, String string){
Item itemInBag = game.getBackpack().getItem(string);
Item itemInRoom = game.getCurrentRoom().getItem(string);
if(itemInBag!= null) {
if(itemInBag instanceof Useable){
game.setGameState(((Useable)itemInBag).use(game));
}else game.getUI().println("you can not use item");
}else if(itemInRoom!= null){
if(itemInRoom instanceof Useable){
game.setGameState(((Useable)itemInRoom).use(game));
}else game.getUI().println("this item can not be used");
}else {
game.getUI().println("take the item and use when you need to");
}
}
}
public class Inventory extends AbstractCommand {
public Inventory (String name, String description){
super (name, description);
}
#Override
public void execute (Game game, String string) {
if(((BackpackImpl)game.getBackpack()).getCount()==0) {
game.getUI().println("Backpack is empty");
}else {
for(Item i: game.getBackpack().getItems()) {
game.getUI().println(i.getName());
}
}
}
}
List<Item> items;
int maxCapacity;
public BackpackImpl (int maxCapacity){
this.maxCapacity = maxCapacity;
items = new ArrayList<Item>(5);
}
public BackpackImpl () {
this.maxCapacity = 5;
}
#Override
public boolean add(Item item){
if(items.size()>maxCapacity)
{System.out.println("your backpack canot fit no more\n");
return false;
}
items.add(item);
return true;
}
#Override
public Item remove(String string){
for(Item i: items){
if(((i.getName()).toLowerCase()).equals(string.toLowerCase())) {
items.remove(i);
return i;
}
}
return null;
}
#Override
public Item getItem(String string){
for(Item i: items){
if(((i.getName()).toLowerCase()).equals(string.toLowerCase())) {
return i;
}
}
return null;
}
#Override
public int getCapacity(){
return maxCapacity;
}
#Override
public List<Item> getItems(){
return items;
}
public int getCount (){
return items.size();
}
public void clear(){
items.clear();
}
}
Due to the "if/else (if)" construction in your Get.execute method, a new item is never added via the BackpackImpl.add() method.
Revise your Get.execute method and only print "Item added" when your code has actually called the BackpackImpl.add() method. Also, to keep your code readable, always use {} when using "if/else (if)" constructions. E.g.:
if (temp == null) {
ui.println("Item added\n");
} else if (!(temp instanceof Moveable || game.getBackpack().add(temp))) {
ui.println("item can not be moved");
} else {
game.getCurrentRoom().removeItem(temp);
}

java jlist - AbstractListModel - fireContentsChanged does not work properly

I have a simple ListModel, that is filterable and is used in a JList...
It uses the following code...
public class FilteredListModel extends AbstractListModel
{
private List<LineData> data = null;
private final ArrayList<Integer> indices = new ArrayList<Integer>();
public FilteredListModel()
{
}
public void setData(List<LineData> data)
{
this.data = data;
doFilter();
}
public void doFilter()
{
int oldSize = indices.size();
indices.clear();
if (data != null)
{
int count = data.size();
for (int i = 0; i < count; i++)
{
IFiltererListObject element = (IFiltererListObject) data.get(i);
if (element.isVisible())
indices.add(i);
}
}
fireContentsChanged(this, 0, getSize() - 1);
if (oldSize > getSize())
fireIntervalRemoved(this, getSize(), oldSize - 1);
}
#Override
public int getSize()
{
return indices.size();
}
#Override
public Object getElementAt(int index)
{
return data.get(indices.get(index));
}
#Override
public void addListDataListener(ListDataListener l)
{
// TODO Auto-generated method stub
//doFilter();
}
#Override
public void removeListDataListener(ListDataListener l)
{
// TODO Auto-generated method stub
//doFilter();
}
}
The strange thing about it is, that it is not working, just if I click for example outside the window, the JList with the ListModel get's correctly updated...
What am I missing here?
The problem is that the addListDataListener and removeListDataListener methods are empty. This means the JList can no longer attach its listener to the model. The call fireContentsChanged will do nothing, as the super class isn't aware of any listeners.
Either do not override those methods, or make sure you call super.addListDataListener as well.
#Robin please DYM???
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;
//usage == new JComboBox(new SectionComboBoxModel(new ArrayList());
public class SectionComboBoxModel extends AbstractListModel implements MutableComboBoxModel {
private static final long serialVersionUID = 1L;
private Object selectedItem;
private ArrayList<Object> sections;
public SectionComboBoxModel(ArrayList<Object> arrayList) {
sections = arrayList;
}
#Override
public Object getSelectedItem() {
return selectedItem;
}
#Override
public void setSelectedItem(Object newValue) {
selectedItem = newValue;
}
#Override
public int getSize() {
return sections.size();
}
#Override
public Object getElementAt(int i) {
return sections.get(i);
}
public void setElementAt(Object newValue, int i) {
this.fireContentsChanged(newValue, i, i);
this.sections.set(i, newValue);
}
#Override
public void addElement(Object obj) {
sections.add(obj);
this.fireIntervalAdded(obj, this.getSize() - 1, this.getSize() - 1);
}
#Override
public void removeElement(Object obj) {
this.fireIntervalRemoved(obj, sections.indexOf(obj), sections.indexOf(obj));
sections.remove(obj);
}
#Override
public void insertElementAt(Object obj, int index) {
sections.add(index, obj);
this.fireIntervalAdded(obj, index, index);
}
#Override
public void removeElementAt(int index) {
this.fireIntervalRemoved(sections.get(index), index, index);
sections.remove(index);
}
public void print() {
System.out.println("\nPrinting List");
for (int i = 0; i < this.sections.size(); i++) {
System.out.println(this.sections.get(i));
}
}
public boolean contains(Object o) {
return sections.contains(o);
}
public Object[] toArray() {
return this.sections.toArray();
}
}

Categories

Resources