Searching a Text Field in a JList

Thread Solved

Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Searching a Text Field in a JList

 
0
  #31
Aug 2nd, 2007
OK, I have figured out what is going on ... I think ... it is not currCD or else it would not check each element and give the dialog box, that tells me it is moving through the list.

If I put the cdname in the cdnamefield also, it finds that cd and populates the fields as it should.
Why would I have to put it in both places? It seems to be searching the field, not the list ... which is what I think it is set to do, but not exactly what I want to do ....

does that rambling make any sense?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Searching a Text Field in a JList

 
0
  #32
Aug 2nd, 2007
I am just flat out busted. I have done everything I can think of, and this is the closest I can get toworking correctly.
If I put the cd name in the search field AND the cdnamefield the search will find it and populate everything like it should.

I do not understand it. The only thing I can think of is that my search is looking in the text field itself, and when a match is made then pulling in that element.

here is the code
	//start at first cd
			currCD = 0;

			// compare
			JTextField f = searchField; 
			final String s = f.getText(); 
			
			SwingUtilities.invokeLater( new Runnable() 
				{ 
				public void run() 
				{ 
				try 
					{ 
					for(int i = 0; i < listModel.getSize();  i++) 
					{ 
					
					CdwArtist cdEntry = (CdwArtist)listModel.elementAt(i);
         					
					if(cdNameField.getText().equalsIgnoreCase(s)) 
						{ 
						Inventorylist.setSelectedIndex(i); 
						Inventorylist.scrollRectToVisible( Inventorylist.getCellBounds(i,i) );
					
						CdwArtist newCD = (CdwArtist) listModel.get( currCD );
			
						artistField.setText(newCD.getArtist());
						cdNameField.setText(newCD.getName());	
						itemField.setText(String.valueOf(newCD.getItemno()));
						nstockField.setText(String.valueOf(newCD.getNstock()));
						priceField.setText(formatter.format(newCD.getPrice()));
						}
						else
					   {
						JOptionPane.showMessageDialog(null,"No CD Match Found","TryAgain",JOptionPane.INFORMATION_MESSAGE); 
						} 
					} 
				} 
				catch(StringIndexOutOfBoundsException sie) 
				{} 
				} 
				});
I have "tweaked" on the currCD and if lines above to my wits end ... i think therein the problem lies.

Please someone pull me from the depths of despair.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,536
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 524
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Searching a Text Field in a JList

 
2
  #33
Aug 2nd, 2007
1. Is there any reason for currCD = 0? Do you use it for something?

2. CdwArtist cdEntry = (CdwArtist)listModel.elementAt(i); will retrive data about CD on position "i" of your list.
What is cdNameField.getText() for? Is it the way you retrieve CD name from cdEntry object?
Just checking few lines bellow it is not! cdNameField.setText(newCD.getName());
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,681
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 554
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Searching a Text Field in a JList

 
1
  #34
Aug 2nd, 2007
See comments in the code (prefixed Ez).
This should be a working version, but I just banged it out in a text editor real quick so any syntax errors are yours to enjoy. I also didn't bother changing a couple of things that would make it slightly more efficient (like holding on to the reference of the found CD instead of getting it again) because I didn't want to induce even more confusion over the mechanics.
  1. //start at first cd
  2. //currCD = 0; // Ez: removed, this has nothing to do with the search
  3.  
  4. // compare
  5. //JTextField f = searchField; // Ez: also removed, superfluous
  6. final String searchString = searchField.getText();
  7.  
  8. SwingUtilities.invokeLater( new Runnable()
  9. {
  10. public void run()
  11. {
  12. boolean matchFound=false; // Ez: this is a simple boolean flag for whether match found
  13. for(int i = 0; i < listModel.getSize(); i++)
  14. {
  15. CdwArtist cdEntry = (CdwArtist)listModel.elementAt(i);
  16.  
  17. if(cdEntry.getName().equalsIgnoreCase(searchString)) // Ez: Yes, you were comparing the cdname field against the search field
  18. {
  19. matchFound=true;
  20. currCD = i;
  21. break;
  22. }
  23. }
  24. // Ez: All you have to do now is check the match flag
  25. if (matchFound) {
  26. Inventorylist.setSelectedIndex(i);
  27. Inventorylist.scrollRectToVisible( Inventorylist.getCellBounds(i,i) );
  28.  
  29. CdwArtist newCD = (CdwArtist) listModel.get( currCD );
  30.  
  31. artistField.setText(newCD.getArtist());
  32. cdNameField.setText(newCD.getName());
  33. itemField.setText(String.valueOf(newCD.getItemno()));
  34. nstockField.setText(String.valueOf(newCD.getNstock()));
  35. priceField.setText(formatter.format(newCD.getPrice()));
  36. }
  37. else
  38. {
  39. // Ez: No match
  40. JOptionPane.showMessageDialog(null,"No CD Match Found","TryAgain",JOptionPane.INFORMATION_MESSAGE);
  41. }
  42. }
  43. });
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Searching a Text Field in a JList

 
1
  #35
Aug 2nd, 2007
wow. I thought I was so close and I couldn't have been farther off.
My boolean attempts were WAY screwed up!

Changing to cdEntry was something I had played with in one way or another several times, never with any success, but I could have stayed for 100 years the way I was going and would have never even come close on most of it.

You don't know how much I appreciate this. I am feeling a bit dence right now and will probably stay out of here the rest of the night just to go over things and try to improve my future performance. This is all so new, and I think I am just trying too much for not having any more experience than I do ... at the same time, I will never learn if I do not try this stuff.

If it is any consolation, I only have two more methods I am even going to attempt and then I am done until I get back from Mexico in mid August ... so you will done with me for at least that long! :o)
Last edited by no1zson; Aug 2nd, 2007 at 8:25 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 5777 | Replies: 34
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC