943,861 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 7168
  • Java RSS
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
Aug 2nd, 2007
0

Re: Searching a Text Field in a JList

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?
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 2nd, 2007
0

Re: Searching a Text Field in a JList

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 2nd, 2007
2

Re: Searching a Text Field in a JList

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());
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Aug 2nd, 2007
1

Re: Searching a Text Field in a JList

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.
Java Syntax (Toggle Plain Text)
  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. });
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 2nd, 2007
1

Re: Searching a Text Field in a JList

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 9:25 pm.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Unicode & Java
Next Thread in Java Forum Timeline: Version output





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC