Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I am using a <code>JList</code>, and due to the <code>JList</code> not have a (value,text) (so I can display text and use the value in my code)</p> </blockquote> <p>It's really hard to understand your problem, but I "suspect" for the quoted line that you have a missunderstanding between <code>JList</code> model and text displayed by the <code>JList</code> itself. I think that's why you have a separate <code>List</code>.</p> <p>The model can contain any object you want and the <code>JList</code> can also display a text as you want regardless the object itself. This last task is done by a <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/ListCellRenderer.html" rel="nofollow noreferrer">ListCellRenderer</a>. Take a look to <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/list.html#renderer" rel="nofollow noreferrer">Writing a Custom Cell Renderer</a></p> <p>For instance you can have this class:</p> <pre><code>class Person { String lastName; String name; public Person(String lastName, String name){ this.lastName = lastName; this.name = name; } public String getLastName(){ return this.lastName; } public String getName(){ return this.name; } } </code></pre> <p>Now you want your <code>JList</code> keep <code>Person</code> objects to work with them later. This part is easy just create a <code>ListModel</code> and add elements into it:</p> <pre><code>DefaultListModel model = new DefaultListModel(); model.addElement(new Person("Lennon","John")); model.addElement(new Person("Harrison","George")); model.addElement(new Person("McCartney","Paul")); model.addElement(new Person("Starr","Ringo")); </code></pre> <p>But you want to display the name and last name of each <code>Person</code>. Well you can implement your own <code>ListCellRenderer</code> to do this:</p> <pre><code>JList list = new JList(model); list.setCellRenderer(new DefaultListCellRenderer(){ @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if(value instanceof Person){ Person person = (Person)value; setText(person.getName() + " " + person.getLastName()); } return this; } }); </code></pre> <p>And your <code>JList</code> will show the items as you want:</p> <p><img src="https://i.stack.imgur.com/LcX2J.jpg" alt="enter image description here"> </p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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