4 Replies - 205 Views - Last Post: 10 April 2019 - 12:45 AM Rate Topic: -----

#1 awthz   User is online

  • D.I.C Head

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

Setting Value to TextField from JList

Posted 08 April 2019 - 07:44 PM

Hi mates, good day.

I tried to set the value of my textfield from the values that I have in my JList.
The first value "Dexter" seems fine, however the next value "Anne Smith" returns an error and the rest of them.



JList<String> list = new JList<String>();
		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.getSelectedValuesList());
					 List user3 = list.getSelectedValuesList();
					 System.out.println(user3);
				}
			}
			
		});
		
		
		list.addListSelectionListener(new ListSelectionListener() {
	          public void valueChanged(ListSelectionEvent e) {
	            int i = list.getSelectedIndex();
	            user.setText(i >= -1 ? list.getSelectedValuesList().get(i) : "");
	          }
	        });
		



The error is this.

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at task2.gui$3.valueChanged(gui.java:113)
at java.desktop/javax.swing.JList.fireSelectionValueChanged(JList.java:1804)
at java.desktop/javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1818)
at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:219)
at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:199)
at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:246)
at java.desktop/javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:443)
at java.desktop/javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:453)
at java.desktop/javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:497)
at java.desktop/javax.swing.JList.setSelectionInterval(JList.java:2077)
at java.desktop/javax.swing.plaf.basic.BasicListUI$Handler.adjustSelection(BasicListUI.java:2901)
at java.desktop/javax.swing.plaf.basic.BasicListUI$Handler.mousePressed(BasicListUI.java:2857)
at java.desktop/java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:288)
at java.desktop/java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:287)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6629)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6397)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4544)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.window.dispatchEventImpl(window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at task2.gui$3.valueChanged(gui.java:113)
at java.desktop/javax.swing.JList.fireSelectionValueChanged(JList.java:1804)
at java.desktop/javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1818)
at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:219)
at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:186)
at java.desktop/javax.swing.DefaultListSelectionModel.setValueIsAdjusting(DefaultListSelectionModel.java:723)
at java.desktop/javax.swing.JList.setValueIsAdjusting(JList.java:2152)
at java.desktop/javax.swing.plaf.basic.BasicListUI$Handler.mouseReleased(BasicListUI.java:2958)
at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:298)
at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6397)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.window.dispatchEventImpl(window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
[Anne Smith]

Is This A Good Question/Topic? 0
  • +

Replies To: Setting Value to TextField from JList

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: Setting Value to TextField from JList

Posted 09 April 2019 - 02:31 AM

I'm confused. The code you posted here (which is essentially identical) you showed to be working...
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: Setting Value to TextField from JList

Posted 09 April 2019 - 04:23 AM

Is there a confusion between the selected items in the List and the index of the items in the shown JList? For example if 10 items are shown in the JList and 2 items are selected (items 3 and 5) the indexes into the selected list would be 0 and 1.
I haven't tested this. We need working code to be able to test it.
Was This Post Helpful? 0
  • +
  • -

#4 awthz   User is online

  • D.I.C Head

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

Re: Setting Value to TextField from JList

Posted 09 April 2019 - 06:58 PM

Hi goose, thank you for your response, however I add another code in the program which I wanted to reflect my array values in the JTextboxField when ever i click it.

The first value "Dexter" is working however the other value is not working.

to Norm, ill gonna post the code soon when I get back from school.

Thank you so much for this community.
Cheers
Was This Post Helpful? 0
  • +
  • -

#5 awthz   User is online

  • D.I.C Head

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

Re: Setting Value to TextField from JList

Posted 10 April 2019 - 12:45 AM

I just solve it NormR

instead of
   user.setText(i >= -1 ? list.getSelectedValuesList().get(i) : "");


on line 28

i used this
 user.setText(i >= 0 ? list.getSelectedValue() : "");

which solves the problem.

Thanks for making me realized it.

This post has been edited by awthz: 10 April 2019 - 12:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1