2 Replies - 129 Views - Last Post: 08 April 2019 - 06:29 PM Rate Topic: -----

#1 awthz   User is online

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 73
  • Joined: 13-February 17

JList multiple selection

Posted 08 April 2019 - 06:03 PM

Is it possible to have a multiple selection in the JList?

this is what I made in my JList.

JList list = new JList();
		list.setLayoutOrientation(JList.VERTICAL_WRAP);
		list.setModel(new AbstractListModel() {
			String[] values = new String[] {"Dexter", "Anne Smith", "Rowena", "Nicki", "John Ross", "Brooke Green", "Dwayne Wade", "Christian Luz", "Slim Shady", "James", "Gianniss", "Step", "Kyle", "Korver", "Kevin", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		
		list.addMouseListener((MouseListener) new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				if(evt.getClickCount()==1) {
				System.out.println(list.getSelectedValues());
				}
			}
			
		});



Also I encountered this kind of bug.
the output is this and not the values selected in the JList.

[Ljava.lang.Object;@1787c8b9
[Ljava.lang.Object;@65529b46

This post has been edited by awthz: 08 April 2019 - 06:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: JList multiple selection

#2 awthz   User is online

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 73
  • Joined: 13-February 17

Re: JList multiple selection

Posted 08 April 2019 - 06:28 PM

I actually solve the problem of the output by trying all the suggested code in the program.
Thank you self :)

System.out.println(list.getSelectedValuesList());

This is the code.

however, im still in awe on how to have multi-selection of Values in the JList.

Happy to find some answers in this community.

Cheers and have a great day or night.
Was This Post Helpful? 0
  • +
  • -

#3 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 771
  • View blog
  • Posts: 5,774
  • Joined: 25-December 13

Re: JList multiple selection

Posted 08 April 2019 - 06:29 PM

Quote

the output is this and not the values selected in the JList.

When you get unexpected results from a method, read the API doc to see what is supposed to be returned by the method.
The Strings that were printed were from the toString method of an array (the leading [) and an @ followed by a hex String.
To see the contents of an array, use the Arrays class's toString method to format the array's contents for printing:
  System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1