> Hi all,
>
> I am currently developing a java application that has to deal with it
> reading ina CSV file and populating the data that is inside the CSV
> file into a vector and then places what is inside the Vector into a
> JList.
You can read a File line by line using FileReader and BufferedReader.
After that you can split every line using StringTokenizer. Then it
should be no problem to save this data into a Vector.
> When the user selects an item from the JList and then they press the
> delete button the item that they selected would get removed from the
> JList. Once the item has been removed from the JList I then want the
> application to write the information that is left inside the vector to
> a file without it having any commas or square brackets etc.
You can use the Vector as DataModel of the JList, so you just have to
delete the Item in the Vector. After that you can save it to a file
using FileWriter and BufferedWriter.
> I am fairly new to Java and therefore apologise if this is a simple
> task to do.
>
> Any help in this matter would be highly appreciated.
>
> Thank you
Knute Johnson - 02 Mar 2008 23:45 GMT
>> Hi all,
>>
[quoted text clipped - 16 lines]
> delete the Item in the Vector. After that you can save it to a file
> using FileWriter and BufferedWriter.
That's not correct. From the docs "Attempts to modify the Vector after
constructing the list results in undefined behavior." Removing an
element from the list will probably not remove it from the Vector.
>> I am fairly new to Java and therefore apologise if this is a simple
>> task to do.
>>
>> Any help in this matter would be highly appreciated.
>>
>> Thank you
I would skip the Vector altogether and just use the DefaultListModel.

Signature
Knute Johnson
email s/nospam/knute/
------->>>>>>http://www.NewsDem
christopher_board@yahoo.co.uk - 03 Mar 2008 12:33 GMT
> christopher_bo...@yahoo.co.uk wrote:
> > Hi all,
[quoted text clipped - 26 lines]
>
> - Show quoted text -
Hi all,
Thanks for the replies. I have tried using some code to remove the
Item from the Vector and then repopulate the existing data that is
left in the vector back into the JList. This is the code that I
currently have:
lstComputerExceptions.getModel();
data.removeAllElements();
data.remove(lstComputerExceptions.getSelectedIndex());
lstComputerExceptions.setListData(data);
lstComputerExceptions being the name of the JList and data being the
name of the vector.
However on the line of code that says
data.remove(lstComputerException.getSelectedIndex()); it keeps on
falling over and comes up with a large errors whic is below:
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
at java.util.Vector.remove(Unknown Source)
at
remoteshutdown.ExceptionsList.btnDelete_actionPerformed(ExceptionsList.java:
193)
at
remoteshutdown.ExceptionsList_btnDelete_actionAdapter.actionPerformed(ExceptionsList.java:
238)
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)
What could be wrong.
Any help in this matter would be highly appreciated. Thank you
Flo - 03 Mar 2008 14:39 GMT
>> christopher_bo...@yahoo.co.uk wrote:
>>> Hi all,
[quoted text clipped - 120 lines]
>
> Any help in this matter would be highly appreciated. Thank you
It is like Knute Johnson said, you have to put the data of the CSV file
into a DefaultListModel. For removing an item you can use boolean
removeElement(Object obj).
It could like like this:
DefaultListModel myModel = new DefaultListModel();
myModel.addElement(new CSVDataElement());
myModel.addElement(...);
JList list = new JList(myModel);
//actionPerformed of the deletebutton-listener
public void actionPerformed(ActionEvent evt)
{
CSVDataElement toDelete = ..
myModel.removeElement( toDelete );
}
I hope it will help you
Flo
>I am currently developing a java application that has to deal with it
>reading ina CSV file and populating the data that is inside the CSV
[quoted text clipped - 9 lines]
>I am fairly new to Java and therefore apologise if this is a simple
>task to do.
reading the CSV file can be done with a specialised CSVReader. See
http://mindprod.com/jgloss/csv.html
Normally you don't use Vectors any more, but the faster replacement
ArrayList.
See http://mindprod.com/jgloss/arraylist.html
For how to use a JList see
http://mindprod.com/jgloss/jlist.html
You need to be more speciific on the format of the output file,
but to simply dump the filelds one after another into a text file you
would use a PrintWriter. See
http://mindprod.com/applet/converter.html
to generate sample code.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com