can't get indexOf to find anything?

Using fancy DefaultListModel

Trying to do a search for hello . When I do the following I always get 1 (meaning not found) but I can see it in the list when displayed?

System.out.println( model.indexOf( "hello" );

I am trying to do searches in a model list and when it finds what it is looking for, it will then ask if we should continue searching for more occurances, etc.

But this 1 all the time has me buffaloed?

Any thoughts?

[482 byte] By [pc_nv] at [2007-9-26 1:45:41]
# 1

The indexOf method returns the index of the first occurrence of the argument in this list

It will returns -1 if the object is not found.

Try this example

import javax.swing.*;

public class DefaultModel extends JFrame{

JList jl;

DefaultListModel lm;

public DefaultModel(){

jl = new JList();

lm = new DefaultListModel();

lm.addElement("abc");

lm.addElement("xyz");

lm.addElement("123");

lm.addElement("xxx");

jl.setModel(lm);

getContentPane().add(jl);

setSize(100,100);

setLocation(200,200);

setVisible(true);

System.out.println(lm.indexOf("xxx"));

System.out.println(lm.indexOf("zzz"));

}

public static void main(String[] args){

new DefaultModel();

}

}

Manu

manu176 at 2007-6-29 2:42:30 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

Couple of Pitfalls to be avoided...

1. The Case of the String DOES matter. If the original object is "Hello" and if you try indexOf("hello"), it is going to return -1.

2. Make sure that you are getting the reference to the right model. That is your "model" object is the right ListModel you need to use.

Frankly, it is not possible to tell you what could be the problem by looking at your post.

Repost with more info, preferably the relevant code, if this does not help.

Best Regards,

Manish

javax.pert at 2007-6-29 2:42:30 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3

You snippet is right on.

I think the indexOf must match everything that was addedis this my problem?

For example instead of:

lm.addElement("xxx");

lm.addElement("hello");

lm.addElement("yyy");

I added:

lm.addElement("xxx");

lm.addElement("Bob, hello, how are you?");

lm.addElement("yyy");

I just want to search and find the hello of the above? Like compare positions 5 9 relative to 0.And was looking to do it with:

System.out.println(lm.indexOf("hello"));

Sorry about being a little unclear

Yes, if I add 123 like:

lm.addElement("123");

I will find 123 with the following:

System.out.println(lm.indexOf("123"));

But if I add 1234567890 with:

lm.addElement("1234567890");

I will not find 123 with the following:

System.out.println(lm.indexOf("123"));

I assume what gets added is considered ONE entity (or object) so you have to indexOf the whole value and not a partial value. That makes sense now?!?! I was hoping for a slick way to find partial contents of objects in model lists without having to get each object to a string and then doing string compares? But I dont see another way? (is there?) (don t want to get into document editing stuff)

pc_nv at 2007-6-29 2:42:30 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4
sorry about the funny characters in my post...it won't happen again...(i cut and pated from word...a no-no I know!)the funny looking "y" are either single or double quotes or dashes (-).
pc_nv at 2007-6-29 2:42:30 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5
well....it looks like doing individual model.getelements for each element in the list and then doing string compares on each element gotten is the only way!thanks anyway folks,pcnv
pc_nv at 2007-6-29 2:42:30 > top of Java-index,Archived Forums,New To Java Technology Archive...