Removing an item from a Jlist

  • Follow


Hi all,

I m currently developing a java application where information from a
file is populated into a JList. The user should be able to select an
item from the JList and then once the user presses the delete button
the item that the user selected should therefore be removed from the
JList. How would I go about doing this.

Any help in this matter would be highly appreciated. Thank you
0
Reply christopher_board 2/28/2008 2:10:53 PM

I think this link will satisfy your needs and maybe more.

http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
0
Reply TheBigPJ 2/28/2008 2:47:21 PM


On 28 Feb, 14:47, TheBigPJ <TheBi...@gmail.com> wrote:
> I think this link will satisfy your needs and maybe more.
>
> http://java.sun.com/docs/books/tutorial/uiswing/components/list.html

Hi all. Thanks for the replies. I have had a look at the website
mentioned above by TheBigPJ and have entered the following the code
into my program:
        int index = lstComputerExceptions.getSelectedIndex();
        lstComputerExceptions.remove(index);



            lstComputerExceptions.setSelectedIndex(index);
            lstComputerExceptions.ensureIndexIsVisible(index);
    }

However when I run it always comes up with the following error
message:
ception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
	at java.awt.Container.remove(Unknown Source)
	at
remoteshutdown.ExceptionsList.btnDelete_actionPerformed(ExceptionsList.java:
191)
	at
remoteshutdown.ExceptionsList_btnDelete_actionAdapter.actionPerformed(ExceptionsList.java:
227)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown
Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown
Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.Dialog$1.run(Unknown Source)
	at java.awt.Dialog$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.awt.Dialog.show(Unknown Source)
	at java.awt.Component.show(Unknown Source)
	at java.awt.Component.setVisible(Unknown Source)
	at java.awt.Window.setVisible(Unknown Source)
	at java.awt.Dialog.setVisible(Unknown Source)
	at
remoteshutdown.mainScreen.jMenuOptionsExceptions_actionPerformed(mainScreen.java:
947)
	at remoteshutdown.mainScreen
$mainScreen_jMenuOptionsExceptions_ActionAdapter.actionPerformed(mainScreen.java:
1113)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI
$Handler.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown
Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown
Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
	at java.awt.Container.remove(Unknown Source)
	at
remoteshutdown.ExceptionsList.btnDelete_actionPerformed(ExceptionsList.java:
191)
	at
remoteshutdown.ExceptionsList_btnDelete_actionAdapter.actionPerformed(ExceptionsList.java:
227)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown
Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown
Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.Dialog$1.run(Unknown Source)
	at java.awt.Dialog$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.awt.Dialog.show(Unknown Source)
	at java.awt.Component.show(Unknown Source)
	at java.awt.Component.setVisible(Unknown Source)
	at java.awt.Window.setVisible(Unknown Source)
	at java.awt.Dialog.setVisible(Unknown Source)
	at
remoteshutdown.mainScreen.jMenuOptionsExceptions_actionPerformed(mainScreen.java:
947)
	at remoteshutdown.mainScreen
$mainScreen_jMenuOptionsExceptions_ActionAdapter.actionPerformed(mainScreen.java:
1113)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI
$Handler.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown
Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown
Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

Does anyone have any ideas as to why this could be happening. I have
had a look on the Internet for the error message but have not found
anyhing that seems to be of any relevance.

Any help in this matter would be highly appreciated.

Thank you
0
Reply christopher_board 2/28/2008 3:49:01 PM

On 28 Feb, 15:49, christopher_bo...@yahoo.co.uk wrote:
> On 28 Feb, 14:47, TheBigPJ <TheBi...@gmail.com> wrote:
>
> > I think this link will satisfy your needs and maybe more.
>
> >http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
>
> Hi all. Thanks for the replies. I have had a look at the website
> mentioned above by TheBigPJ and have entered the following the code
> into my program:
>         int index = lstComputerExceptions.getSelectedIndex();
>         lstComputerExceptions.remove(index);
>
>             lstComputerExceptions.setSelectedIndex(index);
>             lstComputerExceptions.ensureIndexIsVisible(index);
>     }

Did you before hand do this?

tempListModel = new DefaultListModel();
tempListModel.addElement("First");
tempListModel.addElement("Second");
tempListModel.addElement("Third");
lstComputerExceptions = new JList(templistModel);

Peter,
0
Reply TheBigPJ 2/28/2008 4:55:57 PM

On Feb 28, 3:49 pm, christopher_bo...@yahoo.co.uk wrote:
> On 28 Feb, 14:47, TheBigPJ <TheBi...@gmail.com> wrote:
>
> > I think this link will satisfy your needs and maybe more.
>
> >http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
>
> Hi all. Thanks for the replies. I have had a look at the website
> mentioned above by TheBigPJ and have entered the following the code
> into my program:
>         int index = lstComputerExceptions.getSelectedIndex();
>         lstComputerExceptions.remove(index);
>
>             lstComputerExceptions.setSelectedIndex(index);
>             lstComputerExceptions.ensureIndexIsVisible(index);
>     }
>
> However when I run it always comes up with the following error
> message:
> ception occurred during event dispatching:
> java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
<snip>

I suspect that the error refers to line of code
= lstComputerExceptions.setSelectedIndex(index);
If you look at the line immediately preceding that one you might see
what may be causing the error...
0
Reply GArlington 2/29/2008 11:53:56 AM

4 Replies
352 Views

(page loaded in 0.109 seconds)

Similiar Articles:













7/15/2012 5:54:24 PM


Reply: