Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Swing - DefaultListModel - Printing all object information, when i only want to print one field
    primarykey
    data
    text
    <p>I have this DefaultListModel</p> <pre><code>DefaultListModel listModel; //constructor does the right hting... etc.. I skipped over a lot of code JList jlist_available_items; .... jlist_available_items= new JList(cartModel); //etc </code></pre> <p>Everything is working almost perfectly the issue is that</p> <pre><code>listModel.addElement(product); </code></pre> <p>if I change it to product.name it will look correctly, but behave wrongly [the object itself won't be accesisble, only the name]</p> <p>Is adding the object to the view, and all I want to add is the object name.</p> <p>When I change it to the name it causes all sorts of issues, because I store the objects in a hashmap, and the hashmap uses objects as keys, not the product.name string.</p> <p>The reason is so that this method can search the hashmap for the right object.</p> <pre><code> for (Product p : store.database.keySet()) { if (jlist_available_items.getSelectedValuesList().contains( (Product) p)) { // if item is selected cart.addItem(p); } } </code></pre> <p>How can I fix this?? I have been trying to fix it and related bugs for almsot two hours = ( !</p> <p>Also sample output is</p> <pre><code>Product [description=descrion test, name=test] </code></pre> <p>That is what it is printing. I just want it to print the name. = (!</p> <p>Also the objects are in a hashmap. I can just iterate through the hashmap until an object has the same name value and then use that, but I don't want to. I want a more proper and scalable solution, namely because I am having so much trouble thinking of one.</p> <p>BY THE WAY! This is a GUI app in Swing! If you want images just ask = )!</p> <p>EDIT: And now nmy list cell renderer is broken! It was working just a moment ago... = (</p> <pre><code> @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Product product = (Product) value; return this; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload