Help!. Can't remove items from Properties class

I have a messaging system built in an applet that stores and reads messages from text files. The main form has a List control of messages (file names) and a TextArea (txtContent) that displays the contents of each file after clicking on it from the list. The contents of each message (text file) are stored in a Properties table

The problem I have is when I remove one message from the list, I have to remove its contents from the Properties table, and that doesn't work, causing my list of msgs and their contents to go out of sync.

This is how I add the items:

private java.awt.List lstMessages = new java.awt.List();

private Properties m_MessagesTable = new Properties();

String SomeMsg;

String SomeContent;

SomeMsg = "msg1";

SomeContent = "test1";

int iKey = lstMessages.getItemCount()+1;

lstMessages.add(SomeMsg, iKey);

m_MessagesTable.put(Integer.toString(iKey), SomeContent);

SomeMsg = "msg2";

SomeContent = "test2";

int iKey = lstMessages.getItemCount()+1;

lstMessages.add(SomeMsg, iKey);

m_MessagesTable.put(Integer.toString(iKey), SomeContent);

When I select a msg from the list, this is how I show its contents:

String sKey = Integer.toString(lstMessages.getSelectedIndex()+1);

txtContent.setText(m_MessagesTable.getProperty(sKey));

And this is how I delete the item after having selected it from the list:

m_MessagesTable.remove(Integer.toString(lstMessages.getSelectedIndex()+1));

lstMessages.remove(lstMessages.getSelectedIndex());

But when I select and delete the first message, the m_MessagesTable goes crazy and the second item from the list can't find its contents from the table anymore. I'm thinking that when I remove the item from lstMessages, the list renumbers the remaining items, which is fine, but the table does not, and that creates a problem.

Anyone can tell me what am I doing wrong?, or how this could be done differently?

Any help will be appreciated.

Guillermo

[2060 byte] By [gmotodda] at [2007-9-19]
# 1

You are correct, the list renumbers automatically.

You should use something like an ArrayList, rather than a properties object, for your messages. This way, both the collection object and the list share the characteristic that they renumber their entries upon removal of an entry, so that both will stay in synch.

Hope this helps!

Cheers!

Jerry Oberle

goberlea at 2007-7-8 > top of java,Core,Core APIs...
# 2

Using the index maintained by the java.awt.list as a key for your hashtable and expecting the hashtable to update itself when the list is updated does not look like a good idea :) Use the String the list uses.

....

someMsg = "msg1";

someContent = "test1";

lstMessages.add(someMsg);

m_MessagesTable.put(someMsg, someContent);

....

String key = lstMessages.getSelectedItem();

txtContent.setText(m_MessagesTable.getProperty(key));

zakariahqa at 2007-7-8 > top of java,Core,Core APIs...
# 3
Thanks a lot. Your answer made a lot of sense right away. I put it into action and it worked, first try.Sorry for the late reply, I had this project on the burner for a while now.Guillermo.
gmotodda at 2007-7-8 > top of java,Core,Core APIs...